I've used Jasypt but I haven't gone the next step of integrating it with 
Hibernate. Regardless, I think this might do the trick...

At the same level as components, pages, css, etc, create a package called 
startup or something like that. It needs to build into WEB-INF/classes/. Then 
put a class in it like this...

package mywebapp.startup;

public class MyHibernateEncyptorInitializer implements ServletContextListener {
        ServletContext servletContext;

        public void contextInitialized(ServletContextEvent servletContextEvent) 
{
                StandardPBEStringEncryptor strongEncryptor = new 
StandardPBEStringEncryptor();
                ... 
                HibernatePBEEncryptorRegistry registry = 
HibernatePBEEncryptorRegistry.getInstance();
                
registry.registerPBEStringEncryptor("strongHibernateStringEncryptor", 
strongEncryptor);
        }

        public void contextDestroyed(ServletContextEvent servletContextEvent) {
                // Nothing to do here
        }
}

...it should compile into the webapp's classesand in web.xml, you'll find you 
have display-name, context-param, filter, and filter-mapping sections...

        <listener>
                
<listener-class>mywebapp.startup.MyHibernateEncryptorInitializer</listener-class>
        </listener>

The encryptor will then be registered into Hibernate when the ServletContext is 
initialized, ie. when the web server starts up.

Geoff

On 03/08/2011, at 5:46 PM, Sigbjørn Tvedt wrote:

> Hi.
> 
> I am trying to make some fields in the database encrypted by using Jasypt
> (Following this tutorial http://www.jasypt.org/hibernate3.html )
> 
> The part where I am stuck are when I am to register the encryptor. Have any
> of you done this and care to share the code or some pointers to how I can
> register the encryptor?
> 
> The example says:
> -----
> Without Spring, we will have to use the HibernatePBEEncryptorRegistry
> singleton directly, registering our encryptor at application initialization
> like this (for example, inside aServletContextListener for a webapp):
> 
>  StandardPBEStringEncryptor strongEncryptor = new
> StandardPBEStringEncryptor();
>  ...
>  HibernatePBEEncryptorRegistry registry =
>      HibernatePBEEncryptorRegistry.getInstance();
>  registry.registerPBEStringEncryptor("strongHibernateStringEncryptor",
> strongEncryptor);
> -----
> 
> 
> Regards
> Sigbjørn Tvedt

Reply via email to