数据库配置文件加密

    xiaoxiao2021-03-25  155

    1.继承spring原有的PropertyPlaceholderConfigurer 方法,这里解密

    package com.hdkj.utils; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /** * 继承PropertyPlaceholderConfigurer定义支持密文版属性的属性配置器 * * @author lyf * */ public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { // 这里的用户名和密码要写database.properties里的字段 private String[] encryptPropNames = { "jdbc.UserName", "jdbc.PassWord" }; @Override protected String convertProperty(String propertyName, String propertyValue) { if (isEncryptProp(propertyName)) { String decryptValue = DESUtils.getDecryptString(propertyValue); System.out.println(propertyName + "解密内容:" + decryptValue); return decryptValue; } else { return propertyValue; } } /** * 判断是否是加密的属性 * * @param propertyName * @return */ private boolean isEncryptProp(String propertyName) { for (String encryptpropertyName : encryptPropNames) { if (encryptpropertyName.equals(propertyName)) return true; } return false; } } 2. package com.hdkj.utils; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class DESUtils { private static Key key; private static String KEY_STR = "myKey";// 密钥 private static String CHARSETNAME = "UTF-8";// 编码 private static String ALGORITHM = "DES";// 加密类型 static { try { KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM); generator.init(new SecureRandom(KEY_STR.getBytes())); key = generator.generateKey(); generator = null; } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } } /** * 对str进行DES加密 * * @param str * @return */ public static String getEncryptString(String str) { BASE64Encoder base64encoder = new BASE64Encoder(); try { byte[] bytes = str.getBytes(CHARSETNAME); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] doFinal = cipher.doFinal(bytes); return base64encoder.encode(doFinal); } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } } /** * 对str进行DES解密 * * @param str * @return */ public static String getDecryptString(String str) { BASE64Decoder base64decoder = new BASE64Decoder(); try { byte[] bytes = base64decoder.decodeBuffer(str); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); byte[] doFinal = cipher.doFinal(bytes); return new String(doFinal, CHARSETNAME); } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } } } 3.在这里运行得到帐号和密码加密以后的字符串

    package com.hdkj.utils; public class DESUtilsTest { public static void main(String[] args) { System.out.println("加密的字符串例如:帐号spfxzd,密码spfxzd"); System.out.println("正在加密...."); String encryptString = DESUtils.getEncryptString("spfxzd"); String encryptString2 = DESUtils.getEncryptString("spfxzd"); System.out.println("加密完成,输出加密结果:"); System.out.println("帐号:" + encryptString); System.out.println("密码:" + encryptString2); System.out.println("正在解密..."); System.out.println("帐号:" + DESUtils.getDecryptString(encryptString)); System.out.println("密码:" + DESUtils.getDecryptString(encryptString2)); } public void test() { System.out.println("加密的字符串例如:帐号root,密码123456"); System.out.println("正在加密...."); String encryptString = DESUtils.getEncryptString("root"); String encryptString2 = DESUtils.getEncryptString("123456"); System.out.println("加密完成,输出加密结果:"); System.out.println("帐号:" + encryptString); System.out.println("密码:" + encryptString2); System.out.println("正在解密..."); System.out.println("帐号:" + DESUtils.getDecryptString(encryptString)); System.out.println("密码:" + DESUtils.getDecryptString(encryptString2)); } } 4.修改数据库配置文件database.properties

    jdbc.Driver=oracle.jdbc.driver.OracleDriver #jdbc.Url=jdbc:oracle:thin:@localhost:1521:orcl #jdbc.Url=jdbc:oracle:thin:@172.26.2.26:1521:orcl #jdbc.Url=jdbc:oracle:thin:@172.26.11.4:1521:orcl #jdbc.Url=jdbc:oracle:thin:@172.26.11.98:1521:orcl #jdbc.Url=jdbc:oracle:thin:@10.240.132.133:1521:orcl #jdbc.Url=jdbc:oracle:thin:@10.229.106.21:1521:orcl #jdbc.Url=jdbc:oracle:thin:@10.227.230.12:1521:database1 jdbc.Url=jdbc:oracle:thin:@10.229.106.82:1521:orcl #jdbc.UserName=spfxzd 原来的用户名 #jdbc.PassWord=spfxzd 原来的密码 jdbc.UserName=vP3kMMHluII= jdbc.PassWord=vP3kMMHluII=

    5.修改spring配置文件,class引用为自己写的EncryptPropertyPlaceholderConfigurer 类

    <bean id="propertyConfigurer" class="com.hdkj.utils.EncryptPropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:conf/database.properties</value> <value>classpath*:conf/hibernate.properties</value> </list> </property> <property name="fileEncoding"> <value>UTF-8</value> </property> </bean> <context:component-scan base-package="com.spring.*" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.Driver}" /> <property name="jdbcUrl" value="${jdbc.Url}" /> <property name="user" value="${jdbc.UserName}" /> <property name="password" value="${jdbc.PassWord}" /> <property name="initialPoolSize" value="3" /> <property name="minPoolSize" value="2" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="300" /> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="60" /> <!--当连 接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3" /> <property name="autoCommitOnClose" value="false" /> </bean>

    转载请注明原文地址: https://ju.6miu.com/read-6803.html

    最新回复(0)