【问题标题】:How Do I use Jasypt with springs autowire?如何将 Jasypt 与弹簧自动装配一起使用?
【发布时间】:2013-03-11 14:18:37
【问题描述】:

我已经看到了一些没有自动接线的解决方案,但我正在尝试让它与自动接线一起使用。自上周以来我一直在尝试这个,但没有找到适合我的解决方案,可能是我没有正确实施它,所以任何帮助都会很棒。

我有一个属性文件myprops.properties

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

在我的 servlet.xml 文件中我替换了

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

    <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>

在我的 service.java 中,我像这样自动连接这些值:

    @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;
    }

这在我没有加密密码时有效,即UserPassword=nonencriptedpassword

但是一旦我将它加密并使用 ENC() 我就会收到以下错误

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

这可能是一些我不知道或忽略的愚蠢简单的事情,但我已经尝试关注Jasypt wiki 和网站上的教程以及xmx1024jayway 上的教程,我确定我使用自动接线找到了一个,但我似乎无法再次找到它。

仅供参考:我使用的是 spring 3.0.4、Spring Security 3.0.4 和 jasypt 1.9

任何帮助都会很棒

更新

我已将环境变量APP_ENCRYPTION_PASSWORD 设置为等于纯文本密码,并将属性文件中的密码设置为返回值 encrypt input=plaintextpassword password=APP_ENCRYPTION_PASSWORD

我现在得到错误

<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
> 

密码加密是通过以下方式完成的

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****

【问题讨论】:

  • 不要自动装配,而是尝试只为每个属性设置类字段并用@Value注释它们。
  • @SotiriosDelimanolis 但是我应该能够使用 Autowire 来完成此任务吗?
  • 我不确定。 @Autowired 应该与 Spring bean 一起使用。您的字符串不会在任何地方声明为 bean。 @Value 用于为字段提供默认值,我认为这就是您要寻找的。​​span>
  • 我对春天不是很有经验,而且不久前有点陷入困境,所以请原谅我的无知,但如果我删除 @Autowired 并将 @Value 留在原处,那就是什么意思?
  • 删除@Autowired,去掉setter方法,只用@Value注释你的私有字段。您可以保留设置器,但注释您的字段而不是注释方法的参数。

标签: java spring spring-security jasypt


【解决方案1】:

这个blog post 有一个在使用@Service 构建的bean 上使用@Value 的好例子。测试使用@Autowire 设置@Service bean。

【讨论】:

    【解决方案2】:
    1. 设置以下变量 Eclipse - 转到运行配置并在新变量键/值下方设置的环境选项卡下 APP_ENCRYPTION_PASSWORD=我的密钥 如果在 UNIX 上运行 - 导出 APP_ENCRYPTION_PASSWORD=MyKey

    2. 像下面这样创建 bean =>

      类路径:/app1.properties 类路径:/app2.properties

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 2013-10-29
      • 2023-03-26
      • 2013-08-23
      • 2017-05-17
      • 1970-01-01
      相关资源
      最近更新 更多