步骤:
1. 创建加密、解密工具类:
package com.hsoft.framework.util;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* 加密解密工具类
*
* @author dwb
*
*/
public class Des {
private static final String Algorithm = "DES"; // 定义 加密算法,可用
// DES,DESede,Blowfish
// src为被加密的数据缓冲区(源)
public static byte[] encryptMode(byte[] keybyte, byte[] src) {
try {
// 生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
// 加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
// keybyte为加密密钥,长度为24字节
// src为加密后的缓冲区
public static byte[] decryptMode(byte[] keybyte, byte[] src) {
try {
// 生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
// 解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
// 转换成十六进制字符串
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
if (n < b.length - 1)
hs = hs + "";
}
return hs.toUpperCase();
}
// 16 进制 转 2 进制
public static byte[] hex2byte(String hex) throws IllegalArgumentException {
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = "" + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}
private static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
// 加密
public static String Encrypt(String str, byte[] key) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
byte[] encrypt = encryptMode(key, str.getBytes());
return byte2hex(encrypt);
}
// 加密
public static byte[] EncryptRetByte(byte[] src, byte[] key) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
byte[] encrypt = encryptMode(key, src);
return encrypt;
}
// 解密
public static String Decrypt(String str, byte[] key) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
byte[] decrypt = decryptMode(key, hex2byte(str));
return new String(decrypt);
}
public static void main(String arg[]) {
String str = "q1w2e3r4t5";//要加密的信息
String strKey = "0002000200020002";
String s3 = Encrypt(str, hex2byte(strKey));//加密
String s4 = Decrypt(s3, hex2byte(strKey));//解密
System.out.println("加密后的信息 "+s3);
System.out.println("解密后的信息 "+s4);
}
}
2.创建解析properties文件 并对系统进行配置:
package com.hsoft.framework.util;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 解析properties文件 并对系统进行配置
*
* @author dwb
*
*/
public class EncryptablePropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private static final String key = "0002000200020002";
protected void processProperties(
ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
System.out
.println("正在解密系统文件...");
try {
Des des = new Des();
// rk----------------------
String rkUserName = props.getProperty("rk.jdbc.username");
if (rkUserName != null) {
String usernameVal = des.Decrypt(rkUserName, des.hex2byte(key));
props.setProperty("rk.jdbc.username", usernameVal);
}
String rkPassword = props.getProperty("rk.jdbc.password");
if (rkPassword != null) {
String passwordVal = des.Decrypt(rkPassword, des.hex2byte(key));
props.setProperty("rk.jdbc.password", passwordVal);
}
String rkUrl = props.getProperty("rk.jdbc.url");
if (rkUrl != null) {
String urlVal = des.Decrypt(rkUrl, des.hex2byte(key));
props.setProperty("rk.jdbc.url", urlVal);
}
String rkDriverClassName = props
.getProperty("rk.jdbc.driverClassName");
if (rkDriverClassName != null) {
String driverClassNameVal = des.Decrypt(rkDriverClassName,
des.hex2byte(key));
props.setProperty("rk.jdbc.driverClassName", driverClassNameVal);
}
// temp----------------------
String usernameTemp = props.getProperty("temp.jdbc.username");
if (usernameTemp != null) {
String usernameVal = des.Decrypt(usernameTemp,
des.hex2byte(key));
props.setProperty("temp.jdbc.username", usernameVal);
}
String passwordTemp = props.getProperty("temp.jdbc.password");
if (passwordTemp != null) {
String passwordVal = des.Decrypt(passwordTemp,
des.hex2byte(key));
props.setProperty("temp.jdbc.password", passwordVal);
}
String urlTemp = props.getProperty("temp.jdbc.url");
if (urlTemp != null) {
String urlVal = des.Decrypt(urlTemp, des.hex2byte(key));
props.setProperty("temp.jdbc.url", urlVal);
}
String driverClassNameTemp = props
.getProperty("temp.jdbc.driverClassName");
if (driverClassNameTemp != null) {
String driverClassNameVal = des.Decrypt(driverClassNameTemp,
des.hex2byte(key));
props.setProperty("temp.jdbc.driverClassName", driverClassNameVal);
}
super.processProperties(beanFactory, props);
} catch (Exception e) {
e.printStackTrace();
throw new BeanInitializationException(e.getMessage());
}
}
}
3.编写properties文件:
jdbc-test.properties
rk.jdbc.driverClassName = 1F4BC23080
rk.jdbc.url = B7766E9B37
rk.jdbc.username = D3
rk.jdbc.password = 6A285
temp.jdbc.driverClassName = 1F4BC
temp.jdbc.url = B7766E9B37CA2C3F656A72
temp.jdbc.username = D328B
temp.jdbc.password = 6A285EC2
4.修改spring-configuration.xml文件
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="propertyConfigurer"
class="com.hsoft.framework.util.EncryptablePropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc-test.properties</value>
</list>
</property>
</bean>
<!-- 创建数据源与数据库链接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- mysql连接 -->
<!-- 以下是调用加密后的方式-->
<property name="driverClass">
<value>${rk.jdbc.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${rk.jdbc.url}</value>
</property>
<property name="user">
<value>${rk.jdbc.username}</value>
</property>
<property name="password">
<value>${rk.jdbc.password}</value>
</property>
<property name="initialPoolSize">
<value>5</value>
</property>
<property name="maxPoolSize">
<value>10</value>
</property>
<property name="minPoolSize">
<value>5</value>
</property>
<property name="acquireIncrement">
<value>5</value>
</property>
<property name="acquireRetryAttempts">
<value>30</value>
</property>
<property name="acquireRetryDelay">
<value>1000</value>
</property>
<property name="testConnectionOnCheckin">
<value>true</value>
</property>
<property name="idleConnectionTestPeriod">
<value>18000</value>
</property>
<property name="maxIdleTime">
<value>25200</value>
</property>
<property name="preferredTestQuery">
<value>SELECT 1</value>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<!-- 配置 JDBC模板 -->
<bean id="jdbcTemplate" class="com.hsoft.framework.util.FrmJdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 配置事务的传播性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<!--修改为false -->
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="allServiceMethod"
expression="execution(* com.hsoft.framework.service.impl.*.*(..))" />
<aop:pointcut id="allServiceMethodMsptMethod"
expression="execution(* com.hsoft.mspt.*.service.impl.*.*(..))" />
<aop:pointcut id="allToolsDaoJdbcMethod"
expression="execution(* com.hsoft.framework.dao.IToolsDaoJdbc.*(..))" />
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="allServiceMethodMsptMethod"
advice-ref="txAdvice" />
<aop:advisor pointcut-ref="allToolsDaoJdbcMethod"
advice-ref="txAdvice" />
</aop:config>
<!-- temp -->
<bean id="dataSourceTemp" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- mysql连接 -->
<property name="driverClass">
<value>${temp.jdbc.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${temp.jdbc.url}</value>
</property>
<property name="user">
<value>${temp.jdbc.username}</value>
</property>
<property name="password">
<value>${temp.jdbc.password}</value>
</property>
<property name="initialPoolSize">
<value>5</value>
</property>
<property name="maxPoolSize">
<value>10</value>
</property>
<property name="minPoolSize">
<value>5</value>
</property>
<property name="acquireIncrement">
<value>5</value>
</property>
<property name="acquireRetryAttempts">
<value>30</value>
</property>
<property name="acquireRetryDelay">
<value>1000</value>
</property>
<property name="testConnectionOnCheckin">
<value>true</value>
</property>
<property name="idleConnectionTestPeriod">
<value>18000</value>
</property>
<property name="maxIdleTime">
<value>25200</value>
</property>
<property name="preferredTestQuery">
<value>SELECT 1</value>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManagerTemp"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSourceTemp" />
</property>
</bean>
<!-- 配置 JDBC模板 -->
<bean id="jdbcTemplateTemp" class="com.hsoft.framework.util.FrmJdbcTemplateTemp">
<property name="dataSource">
<ref bean="dataSourceTemp" />
</property>
</bean>
<!-- 配置事务的传播性 -->
<tx:advice id="txAdviceTemp" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<!--修改为false -->
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="allServiceMethodTemp"
expression="execution(* com.hsoft.framework.service.impl.*.*(..))" />
<aop:pointcut id="allServiceMethodMsptMethodTemp"
expression="execution(* com.hsoft.mspt.*.service.impl.*.*(..))" />
<aop:pointcut id="allToolsDaoJdbcMethodTemp"
expression="execution(* com.hsoft.framework.dao.IToolsDaoJdbc.*(..))" />
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="allServiceMethodMsptMethod"
advice-ref="txAdvice" />
<aop:advisor pointcut-ref="allToolsDaoJdbcMethod"
advice-ref="txAdvice" />
</aop:config>
</beans>