Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jasypt加密不适用于Maven配置文件_Java_Spring_Maven_Jasypt - Fatal编程技术网

Java Jasypt加密不适用于Maven配置文件

Java Jasypt加密不适用于Maven配置文件,java,spring,maven,jasypt,Java,Spring,Maven,Jasypt,我试图让jasypt解密一个(以前加密过的)属性值,它最终将被用来登录数据库。解密工作正常,除了我介绍Maven概要文件时。我有一组特定于环境的本地/dev/prod属性文件 下面是我的spring3配置的相关部分。这是代码示例中最关键的部分:这将驱动如何设置解密以及将什么解密字符串设置为示例伪bean <bean id="jvmVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentPBEC

我试图让jasypt解密一个(以前加密过的)属性值,它最终将被用来登录数据库。解密工作正常,除了我介绍Maven概要文件时。我有一组特定于环境的本地/dev/prod属性文件

下面是我的spring3配置的相关部分。这是代码示例中最关键的部分:这将驱动如何设置解密以及将什么解密字符串设置为示例伪bean

  <bean id="jvmVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentPBEConfig"
         p:password="secret_password_here"/>

  <bean id="jvmConfigurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"
        p:config-ref="jvmVariablesConfiguration"/>

  <bean id="jvmPropertyConfigurer" class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer"
        p:locations-ref="passwordProps">
    <constructor-arg ref="jvmConfigurationEncryptor"/>
  </bean>

  <util:list id="passwordProps">
    <value>classpath:database.properties</value>    
  </util:list>

  <encryption:encryptable-properties id="dbProps" encryptor="jvmConfigurationEncryptor" location="classpath:database.properties"/>

  <bean id="dummy" class="DummyPropertyTest">
    <property name="prop" value="${database.bar}"/>
  </bean>
然后是我的本地属性文件,位于/src/main/resources/properties/local/database.properties:

database.url=jdbc:hsqldb:hsql://localhost/db
database.username=sa
database.password=
database.dialect=MyHSQLDialect
database.driver=org.hsqldb.jdbcDriver
database.show_sql=true
database.bar=ENC(RSuprdBgcpdheiWX0hJ45Q==)
下面是我的示例Springbean代码,只需读取设置为它的属性。如果一切正常,值将被打印到标准输出并解密

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DummyPropertyTest {

    private String prop;

    public String getProp() {
        return prop;
    }

    public void setProp(String prop) {
        this.prop = prop;
    }

    @Value("#{dbProps['database.bar']}")
    public String otherProp;

    public String getOtherProp() {
        return otherProp;
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext-common.xml");
        DummyPropertyTest dpt = (DummyPropertyTest) ctx.getBean("dummy");

        System.out.println("what's my property being set??: "+dpt.getProp());
        System.out.println("otherProp:"+dpt.getOtherProp());
    }
}
如果我调整我的spring配置以读取驻留在主属性文件上的属性,该文件通常只包含每个属性环境覆盖的占位符,那么解密工作正常。但是,试图从本地属性文件中读取加密属性时,解密不起作用。我已经花了相当多的时间试图调整spring配置,希望这可能只是一个类路径问题,但这似乎也没有帮助

如果只针对我需要加密的属性,我是否需要重写Spring如何看待属性前缀和后缀?(如果我这样做,这似乎适用于所有属性,而不仅仅是可加密的属性,因为Jasypt的EncryptablePropertyPlaceHolderConfigure是Spring的PropertyPlaceHolderConfigure的替代品)

这是程序的输出,如果我设置了两个属性文件,如图所示:
我的属性设置是什么??:ENC(RSuprdBgcpdheiWX0hJ45Q==)

如果主属性文件包含加密属性,则程序的输出如下:
设置我的属性是什么??:sa

我不确定问题是Spring还是Jayspt。我不认为这是马文。如果可能的话,我不想抛弃现在的Maven简介

为清晰起见,对运行时示例进行了编辑。

*更新*:如果使用Jasypt-Spring配置方式,我可以验证该值是否正确解密

然后,在我的测试Bean中,我可以连接一个成员,将属性分配给它:

    @Value("#{dbProps['database.bar']}")
    public String otherProp;

这似乎奏效了。但是我真的需要PropertyOverride这个东西来工作,这样我才能正确地完成数据库配置。

我在同事的帮助下找到了解决方案。问题确实在于maven对概要文件的过滤。这是正确的配置文件设置。在那之后,解密工作就像做梦一样。因此,不需要将@Value注释直接单独连接到bean中:直接从Spring配置设置属性效果很好

<profile>
  <id>local</id>
  <properties>
    <build.profile.id>local</build.profile.id>
  </properties>
  <build>
    <filters>
      <filter>src/main/resources/properties/${build.profile.id}/database.properties</filter>
      <filter>src/main/resources/properties/${build.profile.id}/cli.properties</filter>
    </filters>
    <resources>
      <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
        </includes>
        <excludes>
          <exclude>**/*.xml</exclude>
          <exclude>**/local/*.properties</exclude>
          <exclude>**/dev/*.properties</exclude>
          <exclude>**/prod/*.properties</exclude>
        </excludes>
      </resource>
      <resource>
        <filtering>false</filtering>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
    </resources>
  </build>
</profile>

地方的
地方的
src/main/resources/properties/${build.profile.id}/database.properties
src/main/resources/properties/${build.profile.id}/cli.properties
真的
src/main/resources
**/*.物业
**/*.xml
**/本地/*.属性
**/dev/*.properties
**/prod/*.properties
假的
src/main/resources
**/*.xml
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DummyPropertyTest {

    private String prop;

    public String getProp() {
        return prop;
    }

    public void setProp(String prop) {
        this.prop = prop;
    }

    @Value("#{dbProps['database.bar']}")
    public String otherProp;

    public String getOtherProp() {
        return otherProp;
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext-common.xml");
        DummyPropertyTest dpt = (DummyPropertyTest) ctx.getBean("dummy");

        System.out.println("what's my property being set??: "+dpt.getProp());
        System.out.println("otherProp:"+dpt.getOtherProp());
    }
}
    @Value("#{dbProps['database.bar']}")
    public String otherProp;
<profile>
  <id>local</id>
  <properties>
    <build.profile.id>local</build.profile.id>
  </properties>
  <build>
    <filters>
      <filter>src/main/resources/properties/${build.profile.id}/database.properties</filter>
      <filter>src/main/resources/properties/${build.profile.id}/cli.properties</filter>
    </filters>
    <resources>
      <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
        </includes>
        <excludes>
          <exclude>**/*.xml</exclude>
          <exclude>**/local/*.properties</exclude>
          <exclude>**/dev/*.properties</exclude>
          <exclude>**/prod/*.properties</exclude>
        </excludes>
      </resource>
      <resource>
        <filtering>false</filtering>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
    </resources>
  </build>
</profile>