İlkbaharda daha iyi bir yol var: PropertyPlaceholderConfigurer sınıfını kullanın. Eğer mülk yer tutucuya PropertiesPersister bir alt sınıfını belirlerken
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:com/foo/jdbc.properties</value>
</property>
<property name="propertiesPersister">
<bean class="com.mycompany.MyPropertyPersister" />
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
, Bahar
jdbc.properties
yüklemek ve o sınıfı kullanarak dosyanın şifresini. Belki bir şey gibi:
public class MyPropertyPersister extends DefaultPropertiesPersister
{
// ... initializing stuff...
public void load(Properties props, InputStream is) throws IOException
{
Cipher decrypter = getCipher();
InputStream cis = new CipherInputStream(is, decrypter);
super.load(props, cis);
}
public void load(Properties props, Reader reader) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(reader, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
Cipher decrypter = getCipher();
InputStream cis = new CipherInputStream(bais, decrypter);
InputStreamReader realReader = new InputStreamReader(cis);
super.load(props, realReader);
}
public void loadFromXml(Properties props, InputStream is) throws IOException
{
Cipher decrypter = getCipher();
InputStream cis = new CipherInputStream(is, decrypter);
super.loadFromXml(props, cis);
}
private Cipher getCipher()
{
// return a Cipher to read the encrypted properties file
...
}
...
}
Bu yardımcı olur umarım.
DÜZENLEME Eğer Jasypt kullanırsanız, herhangi PropertiesPersister
tanımlamak gerekmez. Gönderen Jasypt documentation:
Jasypt şifrelenmiş değerler içeren dosyaları .properties okuyabilir bu yapılandırma ile ilgili İlkbahar sınıfların bir uygulamasını sağlar (EncryptableProperties sınıfı tarafından yönetilen olanlar gibi) ve Bahar geri kalanına şeffaf bunları işlemek uygulama kuru fasulye Gizli özelliğini şifresini için Bununla
, bu
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/reportsdb
jdbc.username=reportsUser
jdbc.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
gibi jdbc.properties
tanımlayabilir ve Bahar yapılandırma bu
<bean class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg>
<bean class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config">
<bean class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
</bean>
</property>
</bean>
</constructor-arg>
<property name="locations">
<list>
<value>/WEB-INF/classes/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Bu yol gibi olabilir, parolayı koyabilirsiniz uygulamayı başlattığınızda ve daha sonra ayıkladığınızda bir ortam değişkeninde.
Çok yararlı. Sadece önemsiz bir düzeltme, özellikler dosyasında jdbc.driver ama fasulye tanımında $ {jdbc.driverClassName}. – jbird