SSM实现mysql数据库账号密码密文登录功能
程序员文章站
2022-06-27 15:00:17
引言
咱们公司从事的是信息安全涉密应用的一些项目研发一共有分为三步,相比较于一般公司和一般的项目,对于信息安...
引言
咱们公司从事的是信息安全涉密应用的一些项目研发一共有分为三步,相比较于一般公司和一般的项目,对于信息安全要求更加严格,领导要求数据量和用户的用户名及密码信息都必需是要密文配置和存储的,这就涉及到jdbc.properties文件中的数据库的用户名和密码也是一样的,需要配置问密文,在连接的时候再加载解密为明文进行数据库的连接操作,以下就是实现过程,一共有分为三步。
一、创建desutil类
提供自定义密钥,加密解密的方法。
package com.hzdy.dcad.common.util; import sun.misc.base64decoder; import sun.misc.base64encoder; import javax.crypto.cipher; import javax.crypto.keygenerator; import java.security.key; import java.security.securerandom; /** * created by wongy on 2019/8/8. */ public class desutil { private static key key; //自己的密钥 private static string key_str = "mykey"; static { try { keygenerator generator = keygenerator.getinstance("des"); securerandom securerandom = securerandom.getinstance("sha1prng"); securerandom.setseed(key_str.getbytes()); generator.init(securerandom); key = generator.generatekey(); generator = null; } catch (exception e) { throw new runtimeexception(e); } } /** * 对字符串进行加密,返回base64的加密字符串 * * @param str * @return * @see [类、类#方法、类#成员] */ public static string getencryptstring(string str) { base64encoder base64encoder = new base64encoder(); try { byte[] strbytes = str.getbytes("utf-8"); cipher cipher = cipher.getinstance("des"); cipher.init(cipher.encrypt_mode, key); byte[] encryptstrbytes = cipher.dofinal(strbytes); return base64encoder.encode(encryptstrbytes); } catch (exception e) { throw new runtimeexception(e); } } /** * 对base64加密字符串进行解密 * */ public static string getdecryptstring(string str) { base64decoder base64decoder = new base64decoder(); try { byte[] strbytes = base64decoder.decodebuffer(str); cipher cipher = cipher.getinstance("des"); cipher.init(cipher.decrypt_mode, key); byte[] encryptstrbytes = cipher.dofinal(strbytes); return new string(encryptstrbytes, "utf-8"); } catch (exception e) { throw new runtimeexception(e); } } public static void main(string[] args) { string name = "dbuser"; string password = "waction2016"; string encryname = getencryptstring(name); string encrypassword = getencryptstring(password); system.out.println("encryname : " + encryname); system.out.println("encrypassword : " + encrypassword); system.out.println("name : " + getdecryptstring(encryname)); system.out.println("password : " + getdecryptstring(encrypassword)); } }
二、 创建encryptpropertyplaceholderconfigurer类
建立与配置文件的关联。
package com.hzdy.dcad.common.util; import org.springframework.beans.factory.config.propertyplaceholderconfigurer; public class encryptpropertyplaceholderconfigurer extends propertyplaceholderconfigurer { //属性需与配置文件的key保持一直 private string[] encryptpropnames = {"jdbc.username", "jdbc.password"}; @override protected string convertproperty(string propertyname, string propertyvalue) { //如果在加密属性名单中发现该属性 if (isencryptprop(propertyname)) { string decryptvalue = desutil.getdecryptstring(propertyvalue); system.out.println(decryptvalue); return decryptvalue; } else { return propertyvalue; } } private boolean isencryptprop(string propertyname) { for (string encryptname : encryptpropnames) { if (encryptname.equals(propertyname)) { return true; } } return false; } }
三、 修改配置文件 jdbc.properties
#加密配置之前 #jdbc.driver=com.mysql.jdbc.driver #jdbc.user=root #jdbc.password=root #jdbc.url=jdbc:mysql://localhost:3306/bookstore #加密配置之后 jdbc.driver=com.mysql.jdbc.driver jdbc.user=ov4j7fkiczy= jdbc.password=ov4j7fkiczy= jdbc.url=jdbc:mysql://localhost:3306/bookstore
四、 修改spring-content.xml配置文件
将spring-context中的 <context:property-placeholder location="classpath:.properties" /> 修改为 <bean class="com.hzdy.dcad.common.util.encryptpropertyplaceholderconfigurer"p:locations="classpath:*.properties"/> //注意只能存在一个读取配置文件的bean,否则系统只会读取最前面的
注意:如果发现配置密文的username
和password
可以加载并解密成功,但是最后连接的时候还是以密文连接并报错,这可能涉及到内存预加载的问题,项目一启动,程序会加密密文的用户名和密码,就算最后解密成功了,最后连接数据库读取的却还是密文,这时候我们可以自己重写连接池的方法,让spring-content.xml
加载重写的连接池方法,并在连接的时候再提前进行解密。
package com.thinkgem.jeesite.common.encrypt; import java.sql.connection; import java.sql.sqlexception; import java.util.properties; import javax.security.auth.callback.passwordcallback; import com.alibaba.druid.util.druidpasswordcallback; /** */ @suppresswarnings("serial") public class druiddatasource extends com.alibaba.druid.pool.druiddatasource { public physicalconnectioninfo createphysicalconnection() throws sqlexception { string url = this.geturl(); properties connectproperties = getconnectproperties(); string user; if (getusercallback() != null) { user = getusercallback().getname(); } else { user = getusername(); } //des解密 user = desutils.getdecryptstring(user); string password = desutils.getdecryptstring(getpassword()); passwordcallback passwordcallback = getpasswordcallback(); if (passwordcallback != null) { if (passwordcallback instanceof druidpasswordcallback) { druidpasswordcallback druidpasswordcallback = (druidpasswordcallback) passwordcallback; druidpasswordcallback.seturl(url); druidpasswordcallback.setproperties(connectproperties); } char[] chars = passwordcallback.getpassword(); if (chars != null) { password = new string(chars); } } properties physicalconnectproperties = new properties(); if (connectproperties != null) { physicalconnectproperties.putall(connectproperties); } if (user != null && user.length() != 0) { physicalconnectproperties.put("user", user); } if (password != null && password.length() != 0) { physicalconnectproperties.put("password", password); } connection conn; long connectstartnanos = system.nanotime(); long connectednanos, initednanos, validatednanos; try { conn = createphysicalconnection(url, physicalconnectproperties); connectednanos = system.nanotime(); if (conn == null) { throw new sqlexception("connect error, url " + url + ", driverclass " + this.driverclass); } initphysicalconnection(conn); initednanos = system.nanotime(); validateconnection(conn); validatednanos = system.nanotime(); setcreateerror(null); } catch (sqlexception ex) { setcreateerror(ex); throw ex; } catch (runtimeexception ex) { setcreateerror(ex); throw ex; } catch (error ex) { createerrorcount.incrementandget(); throw ex; } finally { long nano = system.nanotime() - connectstartnanos; createtimespan += nano; } return new physicalconnectioninfo(conn, connectstartnanos, connectednanos, initednanos, validatednanos); } }
修改spring-content.xml文件的数据库连接数配置
#修改之前 <!-- <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource" init-method="init" destroy-method="close"> --> #修改之后 <bean id="datasource"class="com.thinkgem.jeesite.common.encrypt.druiddatasource" init-method="init" destroy-method="close"> <!-- 数据源驱动类可不写,druid默认会自动根据url识别driverclass --> <property name="driverclassname" value="${jdbc.driver}" /> <!-- 基本属性 url、user、password --> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
至此,数据库密文配置连接就完成了!
总结
以上所述是小编给大家介绍的ssm实现mysql数据库账号密码密文登录功能,希望对大家有所帮助