Recently many companies because of security problems by various torture, our company in this regard, we need to encrypt the database connection, on the Internet easily found a solution, here to share to everyone, but the content of this solution has also aroused my thinking about the program!
Solution:Environmental Requirements:
Spring Management database Connection
Configuration Steps1. Configure the Spring file
<span style= "FONT-SIZE:18PX;" ><!-cryptographic decryption Class--><!-cryptographic decryption class: Because it inherits the configuration class, it can change the configuration rule--><beanclass= " Com.zlwy.common.util.ZLWYPropertyPlaceholderConfigurer "> <propertyname=" Location "> & lt;! -Before loading this profile, go to encryption class-<value>classpath:config.properties</value> </property> <propertyname= "fileencoding" > <value>utf-8</value> </property ></bean><bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" Destroy-meth od= "Close" > <propertyname= "Driverclassname" > <!--<value>com.microsoft.sqlse Rver.jdbc.sqlserverdriver</value>-<!--<value>com.mysql.jdbc.Driver</value> -<!--<value>oracle.jdbc.driver.OracleDriver</value>-< Value>${classstring}</value&gT </property> <property name= "url" > <!--Database profile read from configuration file-- <value>${ConnectionString}</value> <!--<value>jdbc:mysql://localhost:3306/frame? Useoldaliasmetadatabehavior=true</value>--> <!--<value>jdbc:oracle:thin:@ (DESCRIPTION = (address_list= (address= (PROTOCOL =tcp) (host=localhost) (port=1521)) (Load_balance=yes) (connect_data=) (SERVICE= Dedicated) (SERVICE_NAME=ORCL)) </value>--> <!--<VALUE>JD bc:oracle:thin:@ (description= (address_list= (address= (protocol= TCP) (host=172.20.104.230)) (PORT=1521 Balance=yes)) (Connect_data= (service=dedicated) (SERVICE_NAME=ORCL)) </value> </property> <propertyname= "username" > <value>${username}< ;/value> </property> <propertyname= "Password" > <value>${PassWord}</value> </prop erty> <propertyname= "maxactive" > <value>1000</value> </property> <propertyname= "Maxidle" > <value>500</value> </property> <propertyname= "Ma Xwait "> <value>5000</value> </property> <!--<propertyname=" Validat Ionquery "> <value>select 1 from dual</value> </property>--></bean> ;</span>
2. Write encryption and decryption classes
<span style= "FONT-SIZE:18PX;" >packagecom.zlwy.common.util; Importjava.io.ioexception;importjava.util.properties; Importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer; Importsun.misc.base64decoder;importsun.misc.base64encoder; /** * <p>title: China Railway Corporation Social Security management system [cryptographic configuration in Database configuration]</p> * <p>description: [For security reasons, we will link the database user name password in the configuration file For encryption, * and for logical decryption]</p> * <p>copyright:copyright (c) 2015</p> * <p>company: Iron Institute Electronics </p> * <p>department: Car Department </p> * @author: LUJC * @version: 1.0 * @date 2015-1-16 */publicclass Zlwypropertyplaceholderconfigurer extends Propertyplaceholderconfigurer {@Overr ideprotected void Convertproperties (Properties properties) {try {base64decoder decoder = NE Wbase64decoder (); String classstring = new String (Decoder.decodebuffer (Properties.getproperty ("classstring")); String connectionString = newstring (Decoder.decodebuffer (Properties.getproperty ("connectionString")); String userName = newstring (Decoder.decodebuffer (Properties.getproperty ("UserName")); String password = newstring (Decoder.decodebuffer (Properties.getproperty ("password")); Mapped to our configuration file, this can be changed by the program Properties.put ("Classstring", classstring); Properties.put ("ConnectionString", ConnectionString); Properties.put ("UserName", UserName); Properties.put ("PassWord", PassWord); SYSTEM.OUT.PRINTLN ("Parse database encryption parameters complete ...."); } catch (Ioexceptione) {e.printstacktrace (); System.out.println ("Failed to resolve database encryption parameters ...."); }}public static void Main (string[] args) throwsexception{String msg = "Semssems"; Base64encoder encode = Newbase64encoder (); String Jiami =encode.encode (Msg.getbytes ()); Base64decoder decoder = Newbase64decoder (); String Jiemi = newstring (Decoder.decodebuffer (Jiami)); System.out.println ("Before encryption:" +MSG); System.out.println ("Post-Encryption" +jiami); System.out.println ("Post-decryption" +jiemi); }}</span>
Summary:We always encounter a lot of problems in our work, but do we stop to solve the problem? Many people just stop here, so many people have the ability to solve problems, but it is difficult to accumulate the ability to optimize, and the sensitivity to demand! Just like this, this is not just about encrypting the data source, we are expanding to think about the problem, the essence of this problem is the quantification of fixed characters, that we are expanding, each big company has a lot of backup database, and dynamic switching data source is so implemented? Isn't that part of big data? poor or rich, lack of ability, but ambition!
Java Project accumulation--encryption Hibernate data source Connector