我下了一个方法继承了spring的PropertyPlaceholderConfigurer方法public class MyConfigurer extends PropertyPlaceholderConfigurer{
static Properties pro = new Properties();
protected void processProperties()
throws BeansException {
try {
InputStream in;
in= MyConfigurer.class.getResourceAsStream("/dbconfig.properties"); //得到数据源的properties文件
pro.load(in);
if(in!=null){
in.close();
}
String un = pro.getProperty("USERNAME");//获得数据库名
if(un!=null){
//帐号
pro.setProperty("USERNAME", AESUtil.decrypt(un,"r20100716w"));
FileOutputStream fos=new FileOutputStream("/dbconfig.properties");
pro.store(fos,"");
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
下面是解密类
public class AESUtil{
//加密
public static String encrypt(String content, String password)
{
String encryptResult = null;
try
{
Cipher cipher = AESUtil.initCipher(Cipher.ENCRYPT_MODE, password);
byte[] byteContent = content.getBytes("UTF8");
byte[] result = cipher.doFinal(byteContent);
encryptResult = parseByte2HexStr(result);
}
catch (Exception e){
e.printStackTrace();
}
return encryptResult;
}
/**
* 解密
* @param content
* @param password
* @return
*/
public static String decrypt(String content, String password)
{
String decryptResult = null;
try
{
Cipher cipher = AESUtil.initCipher(Cipher.DECRYPT_MODE, password);
byte[] result = cipher.doFinal(parseHexStr2Byte(content));
return decryptResult = new String(result, "UTF8");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return decryptResult;
}
private static String parseByte2HexStr(byte buf[])
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++)
{
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1)
{
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
private static byte[] parseHexStr2Byte(String hexStr)
{
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++)
{
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
private static Cipher initCipher(int model, String password)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException
{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(model, key);
return cipher;
}
}
下面是spring.xml文件<!-- 读取dataSource数据配置文件 com.roadway.util.MyConfigurer -->
<bean id="processProperties"
class="com.roadway.util.MyConfigurer">
<property name="locations">
<list>
<value>classpath:dbconfig.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<!-- 驱动类 -->
<property name="driverClassName">
<value>${driverClassName}</value>
</property>
<!-- 数据库连接地址 -->
<property name="url">
<value>jdbc:oracle:thin:@${URL}:${PORT}:${NAME}</value>
</property>
<property name="username">
<value>${USERNAME}</value>
</property>
<property name="password">
<value>${PASSWORD}</value>
</property>
</bean>下面是dbconfig.properties文件#Database Settings
#db url地址
URL=44966ECCCE9BB6EC902028A7B1C7CA
#222.73.31.69
#db name
NAME=rwcm
#db login 登录数据库的账号
USERNAME=A8E4667ADA143D75B331E012FB568718
#db login登录数据库的密码
PASSWORD=987A6A3F7EB623CA04B113B7177952EC
#db port
PORT=1521
driverClassName=oracle.jdbc.driver.OracleDriver
现在我想spring访问数据库的时候调用解密类去解密dbconfig.properties文件里面的账号密码和url地址的密文,然后拿解密后的密码,账号,url地址等等去访问数据库,我尝试了很多方法都不可以,秋高手解答,这是怎么回事
static Properties pro = new Properties();
protected void processProperties()
throws BeansException {
try {
InputStream in;
in= MyConfigurer.class.getResourceAsStream("/dbconfig.properties"); //得到数据源的properties文件
pro.load(in);
if(in!=null){
in.close();
}
String un = pro.getProperty("USERNAME");//获得数据库名
if(un!=null){
//帐号
pro.setProperty("USERNAME", AESUtil.decrypt(un,"r20100716w"));
FileOutputStream fos=new FileOutputStream("/dbconfig.properties");
pro.store(fos,"");
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
下面是解密类
public class AESUtil{
//加密
public static String encrypt(String content, String password)
{
String encryptResult = null;
try
{
Cipher cipher = AESUtil.initCipher(Cipher.ENCRYPT_MODE, password);
byte[] byteContent = content.getBytes("UTF8");
byte[] result = cipher.doFinal(byteContent);
encryptResult = parseByte2HexStr(result);
}
catch (Exception e){
e.printStackTrace();
}
return encryptResult;
}
/**
* 解密
* @param content
* @param password
* @return
*/
public static String decrypt(String content, String password)
{
String decryptResult = null;
try
{
Cipher cipher = AESUtil.initCipher(Cipher.DECRYPT_MODE, password);
byte[] result = cipher.doFinal(parseHexStr2Byte(content));
return decryptResult = new String(result, "UTF8");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return decryptResult;
}
private static String parseByte2HexStr(byte buf[])
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++)
{
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1)
{
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
private static byte[] parseHexStr2Byte(String hexStr)
{
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++)
{
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
private static Cipher initCipher(int model, String password)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException
{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(model, key);
return cipher;
}
}
下面是spring.xml文件<!-- 读取dataSource数据配置文件 com.roadway.util.MyConfigurer -->
<bean id="processProperties"
class="com.roadway.util.MyConfigurer">
<property name="locations">
<list>
<value>classpath:dbconfig.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<!-- 驱动类 -->
<property name="driverClassName">
<value>${driverClassName}</value>
</property>
<!-- 数据库连接地址 -->
<property name="url">
<value>jdbc:oracle:thin:@${URL}:${PORT}:${NAME}</value>
</property>
<property name="username">
<value>${USERNAME}</value>
</property>
<property name="password">
<value>${PASSWORD}</value>
</property>
</bean>下面是dbconfig.properties文件#Database Settings
#db url地址
URL=44966ECCCE9BB6EC902028A7B1C7CA
#222.73.31.69
#db name
NAME=rwcm
#db login 登录数据库的账号
USERNAME=A8E4667ADA143D75B331E012FB568718
#db login登录数据库的密码
PASSWORD=987A6A3F7EB623CA04B113B7177952EC
#db port
PORT=1521
driverClassName=oracle.jdbc.driver.OracleDriver
现在我想spring访问数据库的时候调用解密类去解密dbconfig.properties文件里面的账号密码和url地址的密文,然后拿解密后的密码,账号,url地址等等去访问数据库,我尝试了很多方法都不可以,秋高手解答,这是怎么回事
解决方案 »
- 64位的Weblogic10.2在myeclipse中无法正常启动(100分求助)
- request.setAttribute的问题
- struts2上传照片到服务器 隔一会图片就全部消失了
- 怎样自动判断登录角色并转入相应的服务页面?
- 用ClassPathXmlApplicationContext读取Spring配置文件的路径设置问题!
- Middlegen的问题
- 一个在j2ee平台上做的企业网,市面上现在多少钱 ?
- 请教一个基于J2EE的系统设计
- 大侠求救,filter解决TOMCAT中文问题,我错在哪里?
- java heap space异常
- Hibernate的问题
- spring3.0 restful
http://topic.csdn.net/u/20111124/10/bc8ec667-e4cc-4794-b22a-951c530e0695.html