Why?
The connection information is exposed to the risk in the configuration file
How?
First, ready to encrypt after connection information
Encrypt connection information using Druid Self-tool class Configtools
ImportCom.alibaba.druid.filter.config.ConfigTools; Public classDecryptdruid { PublicDecryptdruid () {Super(); //TODO auto-generated Constructor stub } Public Static voidMain (string[] args) {Decryptdruid DP=NewDecryptdruid (); String[] Myuserpass={Url,driverclassname,username,password}; for(inti = 0;i < Myuserpass.length (); i++) {Dp.testdecrypt (Dp.testencrypt (Myuserpass)); } } /*** Decryption of words *@throwsException*/ Publicstring Testdecrypt (String encry) {//decryptionString word=encry; String Decryptword=""; Try{Decryptword=Configtools.decrypt (word); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } System.out.println (Decryptword); returnDecryptword; } /*** Text is encrypted *@throwsException*/ Publicstring Testencrypt (String userpass) {//EncryptString Encryptword = ""; Try{Encryptword=Configtools.encrypt (Userpass); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } System.out.println (Encryptword); returnEncryptword; }}
Decryptdruid
Second, prepare the decryption method class
Inherit Druiddatasource, override parent class connection information set method
ImportCom.alibaba.druid.filter.config.ConfigTools;ImportCom.alibaba.druid.pool.DruidDataSource; @SuppressWarnings ("Serial") Public classEncryptdruiddatasourceextendsdruiddatasource{@Override Public voidseturl (String url) {Try{URL=configtools.decrypt (URL); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } Super. SetUrl (URL); } @Override Public voidsetdriverclassname (String driverclassname) {Try{driverclassname=Configtools.decrypt (driverclassname); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } Super. Setdriverclassname (Driverclassname); } @Override Public voidSetusername (String username) {Try{username=Configtools.decrypt (username); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } Super. Setusername (username); } @Override Public voidSetPassword (String password) {Try{Password=configtools.decrypt (password); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } Super. SetPassword (password); }}
View Code
Third, change the configuration file
Adding encrypted post-connection information to a configuration file
Filters=config
Iv. Changing the DataSource configuration
<bean id= "DataSource" class= "com.**.**. Encryptdruiddatasource "destroy-method=" Close ">
Add to:
<property name= "connectionproperties" value= "config.decrypt=true;"/>
Ok!
However, configuration mybatis will have DatabaseID not found problem, if there is a solution, hope enlighten!
Spring Encrypted DRUID Connection pool (ii)--encryption url,driverclassname, username, password