How Do I use Jasypt with springs autowire?

江枫思渺然 提交于 2019-12-12 01:29:13

问题


I have see some solutions for this without the autowire but I am trying to get it to work with with the auto wire. I have been trying this since last week but found no solutions that worked in my case, it may be that I am not implementing it correctly so any help would be great.

I have a properties file myprops.properties:

Wsdl=someurl
UserName=admin
UserPassword=ENC(encriptedpassword)

and in my servlet.xml file I replaced

<context:property-placeholder location="classpath*:myprops.properties" />

with

    <bean id="propertyPlaceholderConfigurer"
            class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
            <constructor-arg ref="configurationEncryptor" />
            <property name="location" value="classpath:myprops.properties" />
        </bean>
        <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
            <property name="config" ref="environmentVariablesConfiguration" />
        </bean>
        <bean id="environmentVariablesConfiguration"
              class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
              <property name="algorithm" value="PBEWithMD5AndDES" />
              <property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
</bean>

and in my service.java I am auto wiring these values like this:

    @Autowired
    public void setUserPassword(@Value("${UserPassword}") String userPassword) {
        this.userPassword = userPassword;
    }

    @Autowired
    public void setUserName(@Value("${UserName}") String userName) {
        this.userName = userName;
    }

    @Autowired
    public void setWsdl(@Value("${Wsdl}") String wsdl) {
        this.wsdl = wsdl;
    }

Thing is this works when I don't have the password encripted i.e. UserPassword=nonencriptedpassword

but as soon as I encripted it and use ENC() I get the following error

weblogic.application.ModuleException: 
    at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1510)
    at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:482)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
    at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119)
    Truncated. see log file for complete stacktrace
Caused By: java.lang.NullPointerException
    at org.jasypt.encryption.pbe.config.SimplePBEConfig.getPasswordCharArray(SimplePBEConfig.java:434)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.resolveConfigurationPassword(StandardPBEByteEncryptor.java:740)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:590)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:705)
    Truncated. see log file for complete stacktrace

This may be down something stupidly simple that I am unaware of or have over looked but I have tried following tutorials on the Jasypt wiki and site as well as tutorials xmx1024 and on jayway and I'm sure I had found one using the autowired but I can't seem to find it again.

FYI: I am using spring 3.0.4, Spring Security 3.0.4 and jasypt 1.9

any help would be Awesome

Update

I have set an Enviorment Variable APP_ENCRYPTION_PASSWORD equal to the plain text password and set the password in the properties file as the returned value of encrypt input=plaintextpassword password=APP_ENCRYPTION_PASSWORD

I now get the Error

<11-Mar-2013 16:33:54 o'clock GMT> <Warning> <HTTP> <BEA-101162> <User defined listener org.springframework.web.context.ContextLoaderListener failed: org.jasypt.exceptions.EncryptionOperationNotPossibleException.
org.jasypt.exceptions.EncryptionOperationNotPossibleException
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:981)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:725)
    at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
    at org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer.convertPropertyValue(EncryptablePropertyPlaceholderConfigurer.java:109)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperty(PropertyResourceConfigurer.java:112)
    Truncated. see log file for complete stacktrace
> 
<11-Mar-2013 16:33:54 o'clock GMT> <Error> <Deployer> <BEA-149265> <Failure occurred in the execution of deployment request with ID '1363019632631' for task '0'. Error is: 'weblogic.application.ModuleException: '
weblogic.application.ModuleException: 
    at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1510)
    at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:482)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
    at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119)
    Truncated. see log file for complete stacktrace
Caused By: org.jasypt.exceptions.EncryptionOperationNotPossibleException
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:981)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:725)
    at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
    at org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer.convertPropertyValue(EncryptablePropertyPlaceholderConfigurer.java:109)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperty(PropertyResourceConfigurer.java:112)
    Truncated. see log file for complete stacktrace
> 
<11-Mar-2013 16:33:54 o'clock GMT> <Error> <Deployer> <BEA-149202> <Encountered an exception while attempting to commit the 1 task for the application 'adminUI_EA'.> 
<11-Mar-2013 16:33:54 o'clock GMT> <Warning> <Deployer> <BEA-149004> <Failures were detected while initiating deploy task for application 'adminUI_EA'.> 
<11-Mar-2013 16:33:54 o'clock GMT> <Warning> <Deployer> <BEA-149078> <Stack trace for message 149004
weblogic.application.ModuleException: :org.jasypt.exceptions.EncryptionOperationNotPossibleException:null
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:981)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:725)
    at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
    at org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer.convertPropertyValue(EncryptablePropertyPlaceholderConfigurer.java:109)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperty(PropertyResourceConfigurer.java:112)
    Truncated. see log file for complete stacktrace
> 

Password encription was done in the following way

C:\Users\jonnie\Desktop\jasypt-1.9.0\bin>encrypt input=****** password=APP_ENCRYPTION_PASSWORD

----ENVIRONMENT-----------------

Runtime: Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM 20.12-b01



----ARGUMENTS-------------------

input: *******
password: APP_ENCRYPTION_PASSWORD



----OUTPUT----------------------

****encriptedPassword****

回答1:


This blog post has a nice example of using @Value on a bean that is build using @Service. The test uses @Autowire to set up the @Service bean.




回答2:


  1. Set the below variable Eclipse - go to run config and under environment tab set below new variable key/value APP_ENCRYPTION_PASSWORD=MyKey If running on UNIX - export APP_ENCRYPTION_PASSWORD=MyKey

  2. Create the beans like below =>

    classpath:/app1.properties classpath:/app2.properties



来源:https://stackoverflow.com/questions/15340892/how-do-i-use-jasypt-with-springs-autowire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!