spring起動時、プロファイルの暗号文を復号します.
シーン:spring項目では、セキュリティのために、データベースのパスワードなど、ファイルの一部の情報を暗号化して設定します.springはこれらのプロファイルをロードする際に、対応する解読アルゴリズムを指定してこれらの構成項目を復号します.実現原理:
1.springのorg.spring frame ewark.beans.factory.co.fig.ProptyPlace holderConfigrer種類を継承し、その中のloadPropertiesメソッドを上書きするとともに、locations属性を宣言する必要があります.この属性はロードする配置ファイルの経路であり、PropertyPlaceholderConfirerクラスでカバーする必要があります.対応するカバレッジ後のsetとget方法を同時に提供する.
1.springのorg.spring frame ewark.beans.factory.co.fig.ProptyPlace holderConfigrer種類を継承し、その中のloadPropertiesメソッドを上書きするとともに、locations属性を宣言する必要があります.この属性はロードする配置ファイルの経路であり、PropertyPlaceholderConfirerクラスでカバーする必要があります.対応するカバレッジ後のsetとget方法を同時に提供する.
package com.jaycn.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.AbstractFileResolvingResource;
import org.springframework.core.io.Resource;
/**
* properties
* @Filename PropertiesLoad.java
*
* @Description
*
* @Version 1.0
*
* @Author MEISEI
*
* @Email
*
* @History
*<li>Author: MEISEI</li>
*<li>Date: 2013-5-30</li>
*<li>Version: 1.0</li>
*<li>Content: create</li>
*
*/
public class PropertiesLoad extends PropertyPlaceholderConfigurer {
/** Comment for <code>propertiesPersister</code>
* properties
* */
private PropertiesProcessor propertiesPersister = new PropertiesProcessor();
private Resource[] locations;
@Override
public void setLocation(Resource location) {
this.locations = new Resource[] { location };
}
@Override
public void setLocations(Resource[] locations) {
this.locations = locations;
}
@Override
protected void loadProperties(Properties props) throws IOException {
if (this.locations != null) {
InputStream is = null;
Reader reader = null;
String filename = null;
for (Resource location : this.locations) {
is = null;
try {
is = location.getInputStream();
reader = new InputStreamReader(is);
filename = (location instanceof AbstractFileResolvingResource) ? location
.getFilename() : null;
if ((filename != null) && (filename.endsWith(".xml"))) {
this.propertiesPersister.loadFromXml(props, is);
} else {
this.propertiesPersister.doLoad(props, reader);
}
} catch (IOException ex) {
System.out.println("");
} finally {
if (is != null)
is.close();
}
}
}
}
}
2.springのorg.spring frame ebook.util.Default PropertiesPersister類を継承して、その中のdoLoad方法をカバーして、構成項目の解読過程はdoLoad方法で実現します.package com.jaycn.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.StringUtils;
/**
* properties ,
* @Filename PropertiesProcessor.java
*
* @Description
*
* @Version 1.0
*
* @Author MEISEI
*
* @Email
*
* @History
*<li>Author: MEISEI</li>
*<li>Date: 2013-5-30</li>
*<li>Version: 1.0</li>
*<li>Content: create</li>
*
*/
public class PropertiesProcessor extends DefaultPropertiesPersister {
/** Comment for <code>decodeKeys</code>
* properties
* */
private static List<String> DEC_VALUE_LIST = new ArrayList<String>();
static {
DEC_VALUE_LIST.add("jdbc.password");
}
@Override
protected void doLoad(Properties props, Reader reader) throws IOException {
BufferedReader in = new BufferedReader(reader);
String line = null;
String nextLine = null;
char firstChar = 0;
while (true) {
line = in.readLine();
if (line == null) {
return;
}
line = StringUtils.trimLeadingWhitespace(line);
if (line.length() > 0) {
firstChar = line.charAt(0);
if ((firstChar != '#') && (firstChar != '!')) {
while (endsWithContinuationMarker(line)) {
nextLine = in.readLine();
line = line.substring(0, line.length() - 1);
if (nextLine != null) {
line = line + StringUtils.trimLeadingWhitespace(nextLine);
}
}
int separatorIndex = line.indexOf("=");
if (separatorIndex == -1) {
separatorIndex = line.indexOf(":");
}
String key = separatorIndex != -1 ? line.substring(0, separatorIndex) : line;
String value = separatorIndex != -1 ? line.substring(separatorIndex + 1) : "";
key = StringUtils.trimTrailingWhitespace(key);
value = StringUtils.trimLeadingWhitespace(value);
if (DEC_VALUE_LIST.contains(key)) {
// ****** ******
value = decodeValue(value);
}
props.put(unescape(key), unescape(value));
}
}
}
}
/**
* value
* @param value value
* @return value
*/
private String decodeValue(String value) {
return DesEncryptDecrypt.decode(value);
}
}
3.springのプロファイルに、propertiesファイルを読み込む解析クラスを配置します.例えば、appication Contect.xmlには、次のような情報が配置されています.<!-- JDBC -->
<bean
class="com.jaycn.util.PropertiesLoad">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
注意:データベースプロファイルの読み込みは、データベース接続情報を設定する前に置くべきです.そうでないと、データベースのパスワードが暗号文であるため、データベースに接続できなくなります.