Spring Encrypted DRUID Connection pool (ii)--encryption url,driverclassname, username, password

Source: Internet
Author: User
Tags decrypt

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.