File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring - Jasypt Integration XML Config Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring - Jasypt Integration XML Config" Watch "Spring - Jasypt Integration XML Config" New topic
Author

Spring - Jasypt Integration XML Config

Vincent Paranpan
Greenhorn

Joined: Sep 16, 2010
Posts: 13
Hi guys,
I just want to ask, how can I externalize into a property file the "algorithm", "password", "stringOutputType" of SimpleStringPBEConfig? Because whenever I try to, I keep having this exception:

Exception:

org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException:${TEST_JASYPT_ALGO} SecretKeyFactory not available
at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:708)
at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553)

Here is my configuration:

jasypt.properties:
TEST_JASYPT_ALGO=PBEWithMD5AndDES
TEST_JASYPT_PASS=Password
TEST_JASYPT_STROUT_TYPE=hexadecimal

database.properties
DB_DRIVER=<driver class here>
DB_URL=<url here>
DB_USER=<user here>
DB_PWD=ENC(<encrypted pass here>)

application-context.xml:


Any ideas?

Thanks,
Vincent
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

Well I guess my first question has nothing to do with the problem but with this type of configuration are you not still storing your password in clear text in a properties file?

maybe consider something like this instead



Here you would set ENCRYPTION_PWD as an environment variable then once the application has started up you would remove it. Now there is truly no way to get the unencrypted value.


Anyway back to your question..

I assume you have read the jasypt page about spring 3 integration but if you haven't take a look

http://www.jasypt.org/spring3.html


What I see is you have two PropertyPlaceholderConfigurers. Jasypts version is a drop in replacement you don't need 2 in your case. However if you do have two I would almost expect you would have some exceptions in your log. You can configure more than one but you need to take additional steps to handle different scenarios. For example you can set the order, and the ignoreUnresolvablePlaceholders to true on all but the last loaded PropertyPlaceholderConfigurer. Alternatively you can set the prefix and suffix on the configurer from ${ and } respectively to something different and access them accordingly using your new prefix/suffix and this also can help avoid conflicts.

However in our case I would just get rid of



and add that location to your other configurer.

on a side note (this is not applicable to EncryptablePropertyPlaceholderConfigurer) but for future reference, you can use a short hand context namespace for declaring this


[How To Ask Questions][Read before you PM me]
Vincent Paranpan
Greenhorn

Joined: Sep 16, 2010
Posts: 13
Hi Bill,

Thanks for the prompt reply, I haven't read the whole thing just some few important notes on how to configure jasypt - spring, but because of this exception I'm trying to read the whole thing.

I totally agree with you on the EnvironmentStringPBEConfig, but as per requirement I need to place all configurations in property files.

So I tried to add order and ignoreUnresolvablePlaceholders in my application context:

application-context.xml


and when I started my application I saw this same exception upon initialization:

org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException: $A{TEST_JASYPT_ALGO} SecretKeyFactory not available
at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:708)
at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553)

Is it ignoring the order of placeholder?

Thanks,
Vincent
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

Well since you are referencing it with the new prefix I don't know that order would matter for this particular scenario anyway. Nothing is standing out at me as incorrect but lets try something else just to rule out this being the issue. Lets remove the springPropertyHolder as I think you can get by fine with just the one.

remove this place holder



and replace your jasyptPropHolder with



If you are still getting errors even with this please scroll up and down in your console and make sure there are not other exceptions your missing and post the entire stack trace back here. (Of course you will have to put your references back to ${} as well)


Vincent Paranpan
Greenhorn

Joined: Sep 16, 2010
Posts: 13
Hi Bill,

Sorry it took me a while to reply. I tried your advice, but unfortunately the same exception happened, I posted the whole exception below.

I'm starting to think that before other property placeholders are loaded the decryption of text comes first along with the configurations of it, that's just an assumption based on tests.

application-context.xml



Whole Exception:

STATUS | wrapper | 2012/07/24 09:38:05 | Launching a JVM...
INFO | jvm 1 | 2012/07/24 09:38:06 | Wrapper (Version 3.2.0) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2012/07/24 09:38:06 |
INFO | jvm 1 | 2012/07/24 09:38:06 | Jul 24, 2012 9:38:06 AM org.apache.catalina.startup.Catalina load
INFO | jvm 1 | 2012/07/24 09:38:06 | INFO: Initialization processed in 475 ms
INFO | jvm 1 | 2012/07/24 09:38:08 | SLF4J: Class path contains multiple SLF4J bindings.
INFO | jvm 1 | 2012/07/24 09:38:08 | SLF4J: Found binding in [jar:file:/D:/lib/slf4j-jdk14-1.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
INFO | jvm 1 | 2012/07/24 09:38:08 | SLF4J: Found binding in [jar:file:/D:/lib/slf4j-simple-1.5.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
INFO | jvm 1 | 2012/07/24 09:38:08 | SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
INFO | jvm 1 | 2012/07/24 09:38:09 | ERROR [WrapperSimpleAppMain] (ContextLoader.java:227) - Context initialization failed
INFO | jvm 1 | 2012/07/24 09:38:09 | org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException: ${TEST_JASYPT_ALGO} SecretKeyFactory not available
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:708)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:705)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer.convertPropertyValue(EncryptablePropertyPlaceholderConfigurer.java:111)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperty(PropertyResourceConfigurer.java:112)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperties(PropertyResourceConfigurer.java:95)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:72)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:623)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:941)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1322)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.StandardHost.start(StandardHost.java:862)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.StandardService.start(StandardService.java:525)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
INFO | jvm 1 | 2012/07/24 09:38:09 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
INFO | jvm 1 | 2012/07/24 09:38:09 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
INFO | jvm 1 | 2012/07/24 09:38:09 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
INFO | jvm 1 | 2012/07/24 09:38:09 | at java.lang.reflect.Method.invoke(Method.java:601)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
INFO | jvm 1 | 2012/07/24 09:38:09 | at net.covalent.tomcat.wrapper.ThreadDumpWrapper.main(ThreadDumpWrapper.java:74)
INFO | jvm 1 | 2012/07/24 09:38:09 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
INFO | jvm 1 | 2012/07/24 09:38:09 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
INFO | jvm 1 | 2012/07/24 09:38:09 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
INFO | jvm 1 | 2012/07/24 09:38:09 | at java.lang.reflect.Method.invoke(Method.java:601)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:197)
INFO | jvm 1 | 2012/07/24 09:38:09 | at java.lang.Thread.run(Thread.java:722)
INFO | jvm 1 | 2012/07/24 09:38:09 | Caused by: java.security.NoSuchAlgorithmException: ${TEST_JASYPT_ALGO} SecretKeyFactory not available
INFO | jvm 1 | 2012/07/24 09:38:09 | at javax.crypto.SecretKeyFactory.<init>(SecretKeyFactory.java:121)
INFO | jvm 1 | 2012/07/24 09:38:09 | at javax.crypto.SecretKeyFactory.getInstance(SecretKeyFactory.java:159)
INFO | jvm 1 | 2012/07/24 09:38:09 | at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:695)
INFO | jvm 1 | 2012/07/24 09:38:09 | ... 44 more
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

I guess after looking at this again you are passing encConfig (in another object) to the constructor of the placeholder. It looks like what is happening here is the SimpleStringPBEConfig is being instantiated before the placeholder (since it is needed for the constructor) so the values are not being substituted, In light of this I think my last post will not work. That does not really explain why our first attempt did not work though. I think I would set some break points and see what is going on. If I get some time later I might investigate it further but I won't have time tonight.
Vincent Paranpan
Greenhorn

Joined: Sep 16, 2010
Posts: 13
Hi Bill,

Yeah that's what I noticed when doing tests on this.

I also tried posting questions in Jasypt Forum but unfortunately no one has replied.

http://forum.jasypt.org/Spring-Jasypt-Integration-XML-Config-org-jasypt-exceptions-EncryptionInitializationException-td5706242.html

If I ever fix this I'll post my solution here.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

This can't be done they way we were trying. You are going to have to register those 2 variables in a PropetySource and access them with SpEL. See this jira https://jira.springsource.org/browse/SPR-490, particularly the last comment that was made.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

Ignore the previous Jira link I posted the wrong wrong one my apologies. Your solution can be found here:
https://jira.springsource.org/browse/SPR-6428

The first link should have been

https://jira.springsource.org/browse/SPR-4902?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

you can take a look at that one as well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring - Jasypt Integration XML Config