0

I'm using annotation based entities. I want to use the jasypt encryption datatypes.

I cannot find anything that clearly indicates how to configure them. My persistance.xml just has references to a datasource - no hibernate specific configs.

Is there any documentation out there on how to accomplish this?

fansonly
  • 1,150
  • 4
  • 14
  • 29

1 Answers1

-1

first, define the encryption type with a @TypeDef annotation, which could be either inside the persisted entity class or inside a @TypeDefs declaration in a separate package-info.java file:

@TypeDef(
    name="encryptedString", 
    typeClass=EncryptedStringType.class, 
    parameters= {
        @Parameter(name="encryptorRegisteredName", value="myHibernateStringEncryptor")
    }
)

...and then simply map property with the already declared type:

   @Type(type="encryptedString")
    public String getAddress() {
        return address;
    }

Providing the encryptor to Hibernate myHibernateStringEncryptor ,This can be binded in two different manners, depending on whether we are using an IoC container like Spring or not.

With Spring

    <bean id="hibernateStringEncryptor"
        class="org.jasypt.hibernate4.encryptor.HibernatePBEStringEncryptor">
        <property name="registeredName">
            <value>strongHibernateStringEncryptor</value>
        </property>
        <property name="algorithm">
            <value>PBEWithMD5AndTripleDES</value>
        </property>
        <property name="password">
        <value>jasypt</value>
    </property>
  </bean>

Without Spring,

StandardPBEStringEncryptor strongEncryptor = new StandardPBEStringEncryptor();
  HibernatePBEEncryptorRegistry registry =
      HibernatePBEEncryptorRegistry.getInstance();
  registry.registerPBEStringEncryptor("strongHibernateStringEncryptor", strongEncryptor);

Documentation here

iamiddy
  • 3,015
  • 3
  • 30
  • 33