Với mùa xuân có cách tốt hơn: sử dụng lớp PropertyPlaceholderConfigurer.
<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>
Khi bạn chỉ định một lớp con của PropertiesPersister trong placeholder tài sản, mùa xuân nạp jdbc.properties
và giải mã các tập tin sử dụng lớp đó. Có thể một cái gì đó như:
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
...
}
...
}
Hy vọng điều đó sẽ hữu ích.
EDIT Nếu bạn sử dụng Jasypt, bạn không cần phải xác định bất kỳ PropertiesPersister
. Từ Jasypt documentation:
Jasypt cung cấp một thực hiện của các lớp này mùa xuân cấu hình liên quan đến có khả năng đọc .properties file với các giá trị được mã hóa (giống như những người bởi lớp EncryptableProperties quản lý) và xử lý chúng một cách minh bạch để phần còn lại của mùa xuân đậu ứng dụng.
Với điều này, bạn có thể xác định jdbc.properties
như thế này
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/reportsdb
jdbc.username=reportsUser
jdbc.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
và cấu hình mùa xuân có thể như thế này
<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>
Bằng cách này, bạn có thể đặt mật khẩu cho giải mã thuộc tính ẩn trong một biến môi trường khi bạn khởi động ứng dụng và hủy bỏ nó sau này.
Rất hữu ích. Chỉ cần chỉnh sửa tầm thường, trong tệp thuộc tính, nó là jdbc.driver nhưng trong định nghĩa bean thì nó là $ {jdbc.driverClassName}. – jbird