0

Fellow SO-er's:

I've been puzzling over this one for a couple of days, and, as of yet, don't have a solution ...

I'm building a Spring Boot web app and what I'd like to be able to do is to activate/deactivate encryption of data fields in my datastore (using the facilities provided by jasypt+spring+hibernate) via activating/deactivating configuration profiles. So that - for development work - I can have data fields stored as clear text, while for production, they would be encrypted.

Currently, I'm doing this via a rather inelegant approach. Specifically, I comment/uncomment code in my package-info.java file where I define two @TypeDefs annotation blocks for the type used for the datastore field- one of which is commented and the other of which is active. Thus, my current package-info.java file is written as follows:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Use this @TypeDefs annotation when dataencrypt configuration profile is active
//@TypeDefs({ @TypeDef(name = com.castlehillgaming.gameshare.model.Ticket.ENCRYPTED_STRING_TYPENAME, typeClass = EncryptedStringType.class, parameters = {
//        @Parameter(name = "encryptorRegisteredName", value = com..evilcorp.evilproject.config.EncryptionConfig.REGISTERED_NAME) }) })
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Use this @TypeDefs annotation when dataencrypt configuration profile is not active
@TypeDefs({
        @TypeDef(name = com.evilcorp.evilproject.model.Ticket.ENCRYPTED_STRING_TYPENAME, typeClass = String.class, parameters = {
                @Parameter(name = "encryptorRegisteredName", value = com..evilcorp.evilproject.config.EncryptionConfig.REGISTERED_NAME) }) })
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com..evilcorp.evilproject.model;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.jasypt.hibernate4.type.EncryptedStringType;

And my @Entity Ticket class contains the following:

@Entity
@EqualsAndHashCode(of = { "ticketId" })
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
public class Ticket implements Serializable {

    ...

    @Column(unique = true, nullable = false)
    @Type(type = ENCRYPTED_STRING_TYPENAME)
    private @Getter String ticketId;

    ...
}

I'm hoping that I can devise something that will allow me to reduce my package-info.java file to the following:

@TypeDefs({
        @TypeDef(name = com.evilcorp.evilproject.model.Ticket.ENCRYPTED_STRING_TYPENAME, typeClass = com.evilcorp.evilproject.config.MyTicketDataFieldString.class, parameters = {
                @Parameter(name = "encryptorRegisteredName", value = com..evilcorp.evilproject.config.EncryptionConfig.REGISTERED_NAME) }) })

package com..evilcorp.evilproject.model;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

And define two distinct versions of MyTicketDataFieldString class based on the state of Spring Boot Configuration Profiles. E.g.,

@Configuration
@Profile("dataencrypt")
public class MyTicketDataFieldString extends EncryptedStringType {}

and

@Configuration
@Profile("!dataencrypt")
public class MyTicketDataFieldString implements CharSequence { ... }

where the CharSequence implementation behaves like a vanilla java.lang.String.

But, this won't work because I'll have to define the same class twice in a specific package.

Any ideas on how this can be done (or something equivalent) would be much appreciated.

acscheiner
  • 21
  • 6

0 Answers0