簡體   English   中英

Jasypt加密不適用於Maven配置文件

[英]Jasypt Encryption not working with Maven profiles

我正在嘗試讓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>

在我的一個maven poms中,在這里指定配置文件。

  ...

  <profiles>
    <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>
        </filters>
        <resources>
          <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <includes>
              <include>**/*.properties</include>
              <include>**/*.xml</include>
            </includes>
          </resource>
        </resources>
      </build>
    </profile>

    <!--dev and prod profiles follow this in a similar pattern -->

....

我正在使用jasypt版本1.9.1:

<dependency>
  <groupId>org.jasypt</groupId>
  <artifactId>jasypt-spring3</artifactId>
  <version>1.9.1</version>
</dependency>

我在/ src / main / resources中設置了一個主數據庫屬性文件(database.properties),該文件具有以下占位符屬性:

database.url=${database.url}
database.username=${database.username}
database.password=${database.password}
database.dialect=${database.dialect}
database.driver=${database.driver}
database.show_sql=${database.show_sql}
database.bar=${database.bar}

然后這是我的本地屬性文件,位於/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==)

這是我的示例spring bean代碼,只需讀取設置的屬性即可。 如果一切正常,則將值打印到stdout解密。

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的EncryptablePropertyPlaceholderConfigurer可以替代Spring的PropertyPlaceholderConfigurer。)

如果按照我的說明設置了兩個屬性文件,則這是程序的輸出: what's my property being set??: ENC(RSuprdBgcpdheiWX0hJ45Q==)

如果我的主屬性文件包含加密的屬性,則這是程序的輸出: what's my property being set??: sa

我不確定問題是Spring還是Jayspt。 我不認為這是Maven。 如果可能的話,我寧願不放棄目前的Maven配置文件。

為了清楚起見,編輯了運行時示例。

* 更新 * :如果我使用Jasypt Spring配置方式,我可以驗證該值是否已正確解密<encryption:encryptable-properties id="dbProps" encryptor="jvmConfigurationEncryptor" location="classpath:database.properties"/>

然后在我的測試Bean中,我可以連接一個成員以為其分配屬性:

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

這似乎有效。 但是我確實需要PropertyOverride來工作,這樣我才能正確地獲取數據庫配置的信息。

我在同事的幫助下找到了解決方案。 問題確實是概要文件的行家過濾。 這是更正的配置文件設置。 在那之后,解密就像夢一樣。 因此,不需要將@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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM