SSM实现mysql数据库账号密码加密连接
程序员文章站
2022-06-02 13:11:43
引言 咱们公司从事的是信息安全涉密应用的一些项目研发一共有分为三步,相比较于一般公司和一般的项目,对于信息安全要求更加严格,领导要求数据量和用户的用户名及密码信息都必需是要密文配置和存储的,这就涉及到jdbc.properties文件中的数据库的用户名和密码也是一样的,需要配置问密文,在连接的时候再 ......
引言
咱们公司从事的是信息安全涉密应用的一些项目研发一共有分为三步,相比较于一般公司和一般的项目,对于信息安全要求更加严格,领导要求数据量和用户的用户名及密码信息都必需是要密文配置和存储的,这就涉及到jdbc.properties文件中的数据库的用户名和密码也是一样的,需要配置问密文,在连接的时候再加载解密为明文进行数据库的连接操作,以下就是实现过程,一共有分为三步。
一、创建desutil类
提供自定义密钥,加密解密的方法。
1 package com.hzdy.dcad.common.util; 2 3 import sun.misc.base64decoder; 4 import sun.misc.base64encoder; 5 import javax.crypto.cipher; 6 import javax.crypto.keygenerator; 7 import java.security.key; 8 import java.security.securerandom; 9 10 /** 11 * created by wongy on 2019/8/8. 12 */ 13 public class desutil { 14 private static key key; 15 //自己的密钥 16 private static string key_str = "mykey"; 17 18 static { 19 try { 20 keygenerator generator = keygenerator.getinstance("des"); 21 securerandom securerandom = securerandom.getinstance("sha1prng"); 22 securerandom.setseed(key_str.getbytes()); 23 generator.init(securerandom); 24 key = generator.generatekey(); 25 generator = null; 26 } catch (exception e) { 27 throw new runtimeexception(e); 28 } 29 } 30 31 /** 32 * 对字符串进行加密,返回base64的加密字符串 33 * 34 * @param str 35 * @return 36 * @see [类、类#方法、类#成员] 37 */ 38 public static string getencryptstring(string str) { 39 base64encoder base64encoder = new base64encoder(); 40 try { 41 byte[] strbytes = str.getbytes("utf-8"); 42 cipher cipher = cipher.getinstance("des"); 43 cipher.init(cipher.encrypt_mode, key); 44 byte[] encryptstrbytes = cipher.dofinal(strbytes); 45 return base64encoder.encode(encryptstrbytes); 46 } catch (exception e) { 47 throw new runtimeexception(e); 48 } 49 50 } 51 52 /** 53 * 对base64加密字符串进行解密 54 * 55 */ 56 public static string getdecryptstring(string str) { 57 base64decoder base64decoder = new base64decoder(); 58 try { 59 byte[] strbytes = base64decoder.decodebuffer(str); 60 cipher cipher = cipher.getinstance("des"); 61 cipher.init(cipher.decrypt_mode, key); 62 byte[] encryptstrbytes = cipher.dofinal(strbytes); 63 return new string(encryptstrbytes, "utf-8"); 64 } catch (exception e) { 65 throw new runtimeexception(e); 66 } 67 68 } 69 70 71 public static void main(string[] args) { 72 string name = "dbuser"; 73 string password = "waction2016"; 74 string encryname = getencryptstring(name); 75 string encrypassword = getencryptstring(password); 76 system.out.println("encryname : " + encryname); 77 system.out.println("encrypassword : " + encrypassword); 78 79 system.out.println("name : " + getdecryptstring(encryname)); 80 system.out.println("password : " + getdecryptstring(encrypassword)); 81 } 82 }
二、 创建encryptpropertyplaceholderconfigurer类
建立与配置文件的关联。
1 package com.hzdy.dcad.common.util; 2 3 import org.springframework.beans.factory.config.propertyplaceholderconfigurer; 4 5 public class encryptpropertyplaceholderconfigurer extends propertyplaceholderconfigurer { 6 //属性需与配置文件的key保持一直 7 private string[] encryptpropnames = {"jdbc.username", "jdbc.password"}; 8 9 @override 10 protected string convertproperty(string propertyname, string propertyvalue) { 11 12 //如果在加密属性名单中发现该属性 13 if (isencryptprop(propertyname)) { 14 string decryptvalue = desutil.getdecryptstring(propertyvalue); 15 system.out.println(decryptvalue); 16 return decryptvalue; 17 } else { 18 return propertyvalue; 19 } 20 21 } 22 23 private boolean isencryptprop(string propertyname) { 24 for (string encryptname : encryptpropnames) { 25 if (encryptname.equals(propertyname)) { 26 return true; 27 } 28 } 29 return false; 30 } 31 }
三、 修改配置文件 jdbc.properties
1 #加密配置之前 2 #jdbc.driver=com.mysql.jdbc.driver 3 #jdbc.user=root 4 #jdbc.password=root 5 #jdbc.url=jdbc:mysql://localhost:3306/bookstore 6 7 #加密配置之后 8 jdbc.driver=com.mysql.jdbc.driver 9 jdbc.user=ov4j7fkiczy= 10 jdbc.password=ov4j7fkiczy= 11 jdbc.url=jdbc:mysql://localhost:3306/bookstore
四、 修改spring-content.xml配置文件
1 将spring-context中的 2 <context:property-placeholder location="classpath:.properties" /> 3 修改为 4 <bean class="com.hzdy.dcad.common.util.encryptpropertyplaceholderconfigurer"p:locations="classpath:*.properties"/> 5 //注意只能存在一个读取配置文件的bean,否则系统只会读取最前面的
注意:如果发现配置密文的username和password可以加载并解密成功,但是最后连接的时候还是以密文连接并报错,这可能涉及到内存预加载的问题,项目一启动,程序会加密密文的用户名和密码,就算最后解密成功了,最后连接数据库读取的却还是密文,这时候我们可以自己重写连接池的方法,让spring-content.xml加载重写的连接池方法,并在连接的时候再提前进行解密。
1 package com.thinkgem.jeesite.common.encrypt; 2 3 import java.sql.connection; 4 import java.sql.sqlexception; 5 import java.util.properties; 6 7 import javax.security.auth.callback.passwordcallback; 8 import com.alibaba.druid.util.druidpasswordcallback; 9 10 /** 11 */ 12 @suppresswarnings("serial") 13 public class druiddatasource extends com.alibaba.druid.pool.druiddatasource { 14 15 public physicalconnectioninfo createphysicalconnection() throws sqlexception { 16 string url = this.geturl(); 17 properties connectproperties = getconnectproperties(); 18 19 string user; 20 if (getusercallback() != null) { 21 user = getusercallback().getname(); 22 } else { 23 user = getusername(); 24 } 25 //des解密 26 user = desutils.getdecryptstring(user); 27 string password = desutils.getdecryptstring(getpassword()); 28 29 passwordcallback passwordcallback = getpasswordcallback(); 30 31 if (passwordcallback != null) { 32 if (passwordcallback instanceof druidpasswordcallback) { 33 druidpasswordcallback druidpasswordcallback = (druidpasswordcallback) passwordcallback; 34 35 druidpasswordcallback.seturl(url); 36 druidpasswordcallback.setproperties(connectproperties); 37 } 38 39 char[] chars = passwordcallback.getpassword(); 40 if (chars != null) { 41 password = new string(chars); 42 } 43 } 44 45 properties physicalconnectproperties = new properties(); 46 if (connectproperties != null) { 47 physicalconnectproperties.putall(connectproperties); 48 } 49 50 if (user != null && user.length() != 0) { 51 physicalconnectproperties.put("user", user); 52 } 53 54 if (password != null && password.length() != 0) { 55 physicalconnectproperties.put("password", password); 56 } 57 58 connection conn; 59 60 long connectstartnanos = system.nanotime(); 61 long connectednanos, initednanos, validatednanos; 62 try { 63 conn = createphysicalconnection(url, physicalconnectproperties); 64 connectednanos = system.nanotime(); 65 66 if (conn == null) { 67 throw new sqlexception("connect error, url " + url + ", driverclass " + this.driverclass); 68 } 69 70 initphysicalconnection(conn); 71 initednanos = system.nanotime(); 72 73 validateconnection(conn); 74 validatednanos = system.nanotime(); 75 76 setcreateerror(null); 77 } catch (sqlexception ex) { 78 setcreateerror(ex); 79 throw ex; 80 } catch (runtimeexception ex) { 81 setcreateerror(ex); 82 throw ex; 83 } catch (error ex) { 84 createerrorcount.incrementandget(); 85 throw ex; 86 } finally { 87 long nano = system.nanotime() - connectstartnanos; 88 createtimespan += nano; 89 } 90 91 return new physicalconnectioninfo(conn, connectstartnanos, connectednanos, initednanos, validatednanos); 92 } 93 }
修改spring-content.xml文件的数据库连接数配置
1 #修改之前 2 <!-- <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource" init-method="init" destroy-method="close"> --> 3 4 #修改之后 5 <bean id="datasource"class="com.thinkgem.jeesite.common.encrypt.druiddatasource" 6 init-method="init" destroy-method="close"> 7 <!-- 数据源驱动类可不写,druid默认会自动根据url识别driverclass --> 8 <property name="driverclassname" value="${jdbc.driver}" /> 10 <!-- 基本属性 url、user、password --> 11 <property name="url" value="${jdbc.url}" /> 12 <property name="username" value="${jdbc.username}" /> 13 <property name="password" value="${jdbc.password}" /> 14 15 </bean>
至此,数据库密文配置连接就完成了!