zoukankan      html  css  js  c++  java
  • Springboot整合Hikari数据库连接池,密码加密

    1.application.yml配置

     1 spring:
     2   datasource:
     3     jdbcUrl: jdbc:mysql://127.0.0.1:3306/jby?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
     4     username: root
     5     password: 'f687101570bae7ce4d313c2b4440f4ae'
     6     #自动提交
     7     auto-commit: true
     8     #最小连接
     9     minimum-idle: 100
    10     #最大连接
    11     maximum-pool-size: 200
    12     #最大空闲时间
    13     idle-timeout: 60000
    14     #连接池名
    15     pool-name: DatebookHikariCP
    16     #最大生命周期
    17     max-lifetime: 900000
    18     #连接超时时间
    19     connection-timeout: 15000
    20     #心跳检测
    21     connection-test-query: SELECT 'x' FROM DUAL

    2. 构建UmspscDataSource类,继承HikariDataSource类

     1 @Slf4j
     2 public class UmspscDataSource extends HikariDataSource {
     3     private String passwordDis;
     4     /**
     5      * 密匙
     6      */
     7     private final static String PKEY ="1234565437892132";
     8     @Override
     9     public String getPassword(){
    10 
    11         if(StringUtils.isNotBlank(passwordDis)){return passwordDis;}
    12         String encPassword = super.getPassword();
    13         if(null==encPassword){
    14             return null;
    15         }
    16         log.info("数据库密码加解密,{"+encPassword+"}");
    17         try{
    18             //  密文解密,解密方法可以修改
    19             String key = HexUtil.encodeHexStr(PKEY);
    20             SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes());
    21             passwordDis = aes.decryptStr(encPassword, CharsetUtil.CHARSET_UTF_8);
    22             return passwordDis;
    23         }catch (Exception e){
    24             log.error("数据库密码解密出错,{"+encPassword+"}");
    25             log.error(LogUtil.e(e));
    26             throw new AppException("数据库密码解密失败!", e);
    27         }
    28     }
    29 }

    3.初始化DataSource类

     1 @Component
     2 public class CommonBeanFactory {
     3 
     4     @Bean(name = "dataSource", autowire = Autowire.NO)
     5     @Primary
     6     @ConfigurationProperties(ignoreUnknownFields = false,prefix="spring.datasource")
     7     public HikariDataSource dataSource() {
     8         HikariDataSource druidDataSource = new UmspscDataSource();
     9         return druidDataSource;
    10     }
    11 }

    *******************************

    构建密文

     1 @Slf4j
     2 public class Main {
     3 
     4     public static void main(String[] args) {
     5         //明文
     6         String content = "123456";
     7         //密匙
     8         String pkey = "1234565437892132";
     9         log.info("密匙:" + pkey);
    10         String key = HexUtil.encodeHexStr(pkey);
    11         //构建
    12         SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes());
    13 
    14         //加密为16进制表示
    15         String encryptHex = aes.encryptHex(content);
    16         log.info("密文:" + encryptHex);
    17         //解密为字符串
    18         String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
    19         log.info("明文:" + decryptStr);
    20     }
    21 }
  • 相关阅读:
    solr 简要笔记
    JQuery select 编程时选中原有的值
    java设计模式图
    java设计模式
    qq空间微博等更多社交平台分享
    API调用开发demo
    fastJson
    小程序开发
    史上最全的机器学习资料(上)
    大数据”学习资源(下)
  • 原文地址:https://www.cnblogs.com/ljb161108/p/11334451.html
Copyright © 2011-2022 走看看