最終的にデータベースへのログインに使用される (以前に暗号化された) プロパティ値を jasypt で復号化しようとしています。Maven プロファイルを導入する場合を除いて、復号化は正常に機能します。環境固有のプロパティ ファイルの local/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 pom の 1 つで、ここでプロファイルを指定します。
...
<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 がプロパティのプレフィックスとサフィックスをどのように見るかをオーバーライドする必要がありますか? (そうすると、Jasypt の EncryptablePropertyPlaceholderConfigurer は Spring の PropertyPlaceholderConfigurer のドロップイン置換であるため、暗号化可能なプロパティだけでなく、すべてのプロパティに適用できるように見えます)。
説明したように 2 つのプロパティ ファイルを設定した場合のプログラムの出力は次のとおりです。
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 が機能することが本当に必要です。