码迷,mamicode.com
首页 > 其他好文 > 详细

Druid密码加密

时间:2019-02-03 18:17:21      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:目录   []   解密   xid   test   filter   ret   username   public   

pom里引用:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
</dependency>

加密解密示例:

import com.alibaba.druid.filter.config.ConfigTools;

public class Druid密码加密解密 {

    public static void main(String[] args) throws Exception {
        //第一种加密方法
        //到druid-1.1.10.jar目录下打开cmd窗口,执行以下命令为密码ZHUwen12加密,随后获得公钥public key
        //java -cp druid-1.1.10.jar com.alibaba.druid.filter.config.ConfigTools ZHUwen12
        
        //第一种解密方法
        String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALESqBkd/4Kne+NVwVLirUBsXKTYlsOJjgNQ7bcwzbPn0c0Wv6h+KXPHbwkOJiuz6b4H+GBcB+HuELwiTrTB4dsCAwEAAQ==";
        String encryptPassword = "C3Kcc9g8m/xel7VgakJKXFJcCl7IiOc65r0o5hpi0qP4LjQ46s82T1UeRHfwzZfP0QC4GVpv7j3H5YfxckBnqg==";
        String decryptPassword = ConfigTools.decrypt(publicKey, encryptPassword);
        System.out.println("decryptPassword==="+decryptPassword);
        
        //第二种加密方法
        String pwd = "ZHUwen12";
        String encryptPwd = ConfigTools.encrypt(pwd);
        System.out.println("加密后:"+encryptPwd);
        
        //第二种解密方法
        String decryptPwd = ConfigTools.decrypt(encryptPwd);
        System.out.println("解密后:"+decryptPwd);

    }

}

在atomikos里使用密码加密时,解密代码这样写:

@Primary
    @Bean(name = "dataSource1")
    public DataSource testDataSource(DBConfig1 testConfig) throws SQLException {
        //Atomikos统一管理分布式事务
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();

        //用druidXADataSource方式或者上面的Properties方式都可以
        DruidXADataSource druidXADataSource = new DruidXADataSource();
        druidXADataSource.setConnectionProperties("config.decrypt=true;config.decrypt.key="+testConfig.getPublicKey());
        druidXADataSource.setFilters("config,stat");
        druidXADataSource.setUrl(testConfig.getUrl());
        druidXADataSource.setUsername(testConfig.getUsername());
        druidXADataSource.setPassword(testConfig.getPassword());
        
        xaDataSource.setUniqueResourceName("oracle1");
        xaDataSource.setXaDataSource(druidXADataSource);
        xaDataSource.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
        xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
        xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
        xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());
        xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
        xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
        xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());
        xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
        xaDataSource.setTestQuery(testConfig.getTestQuery());
        
        LOG.info("分布式事物dataSource1实例化成功");
        return xaDataSource;
    }

.

Druid密码加密

标签:目录   []   解密   xid   test   filter   ret   username   public   

原文地址:https://www.cnblogs.com/zhuwenjoyce/p/10350707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!