Druid database configuration for encryption and decryption

Source: Internet
Author: User
Tags decrypt decrypt text
** write the database user name and password in the configuration file, not secure, here with Ali's Druid database connection pool to specify the configuration properties of the custom add solution**
Here is a brief introduction to the project of the SPRINGMVC framework, below a brief introduction to the following configuration: * First step *: If you use MAVEN management project, you can introduce the relevant jar package in the Pom.xml file.
<dependency>
      <groupId>com.alibaba</groupId>
      <artifactid>druid</artifactid >
      <version>1.0.26</version>
  </dependency>

* Step Two *: Add a Properties Profile db.properties in the project to place database configuration information
Connection.driverclass=com.mysql.jdbc.driver
connection.url=jdbc:mysql://localhost:3306/druid?useunicode= True&characterencoding=utf-8
#数据库连接用户名
connection.username= v9wll1ktzlxgo2x8pf2hfktxrj6xa78i8cbmzntcekrhoxvrphs1co8lcfjl3bytkt2c9rfsza3vzbsycpheuw==
#数据库连接密码 
connection.password=qglxhqq1mrycukktrco9q84ogiwescf0le4cyykjerlu2pkqxmbdlbso0qmamnvmot2lv4+rbfa2uun1oxj8yw==# Configure initialization size, minimum, maximum
druid.initialsize=10
#最小连接池数量
druid.minidle=10
#最大连接池数量
druid.maxactive=50
druid.validationquery=select ' x '
#检测连接是否有效的超时时间, units: seconds. The underlying call to the JDBC Statement object's void setquerytimeout (int seconds) method
#配置监控统计拦截的filters
Druid.filters=config,stat, SLF4J
#使用全局监控
druid.useglobaldatasourcestat=true

* Step Three *: Configure the database connection pool and join the database connection pool in the Applicationcontext.xml file.
<bean id= "Xxxdatasource"  name= "Xxxdatasource"  class= "Com.xxx.utils.DecryptDruidSource" init-method= " Init "destroy-method=" close ">
  <property name=" Driverclassname "value=" ${connection.driverclass} "/>
  <property name= "url" value= "${connection.url}"/>
  <property name= "username" value= "${ Connection.username} "/>
  <property name=" password "value=" ${connection.password} "/>
  <property Name= "InitialSize" value= "${druid.initialsize}"/>
  <property name= "Minidle" value= "${druid.minIdle}"/ >
  <property name= "maxactive" value= "${druid.maxactive}"/>
  <property name= "maxwait" value= "${ druid.maxwait} "/>
  <property name=" validationquery "value=" ${druid.validationquery} "/>
  < Property Name= "ConnectionProperties" value= "${druid.connectionproperties}"/>
  <!--configuration Monitoring statistics interception filters-- >
  <property name= "Filters" value= "${druid.filters}"/>
 </bean>

* * Note **:* here is a custom class decryptdruidsource* * Fourth *: Custom Decryptdruidsource class, add and decrypt the specified attribute parameters, passwords are self-encrypting and do not need to be processed
public class Decryptdruidsource extends druiddatasource{
 /**
  * * *
 private static final long Serialversionuid = -395779072965051202l;
 @Override public
 void Setusername (String username) {
  try {
   username = configtools.decrypt (username);// Alibaba decrypt
  } catch (Exception e) {
   e.printstacktrace ();
  }
  Super.setusername (username);
 }
}

You can also encrypt other properties here, overriding the method, such as a URL.
* Step Fifth *: Convert plaintext user name and password to ciphertext.
Write a main method to test and decrypt
public class Decryptdruid {public
    static void Main (string[] args) {
     try {
   testencrypt ();
   Testdecrypt ();
  } catch (Exception e) {
   e.printstacktrace ();
  }
 }
 /**
     * Decrypt text *
     /public
    static void Testdecrypt () throws Exception {
        //decrypt
        String word= " v9wll1ktzlxgo2x8pf2hfktxrj6xa78i8cbmzntcekrhoxvrphs1co8lcfjl3bytkt2c9rfsza3vzbsycpheuw== ";
        String Decryptword = Configtools.decrypt (word);
        System.out.println (Decryptword);
    }
    /**
     * Text encryption *
    /public static void Testencrypt () throws exception{
        //Encrypt
        String context = " Username ";
        String password = "123456";
        String Encryptword = configtools.encrypt (context);
        String password2 = configtools.encrypt (password);
        System.out.println (Encryptword);
        System.out.println (PASSWORD2);
    }
}

The ciphertext obtained here can be placed in the corresponding configuration file db.properties. * Additional *: The fifth step key now uses the PublicKey that comes with the jar package, and of course you can configure the custom PublicKey
Run JAVA-CP d:/druid-1.0.27.jar com.alibaba.druid.filter.config.ConfigTools password in the console and get PublicKey and password Put in the configuration file db.properties, but this way seems to only encrypt one at a time, and then execute once, before the PublicKey will change, cannot decrypt the previous.

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.