1.application.yml配置

spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/jby?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
# 密文
password: 'f687101570bae7ce4d313c2b4440f4ae'
initialSize: 100
minIdle: 100
maxActive: 200
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x' FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: config,stat

2. 构建UmspscDataSource类,继承DruidDataSource类

@Slf4j
public class UmspscDataSource extends DruidDataSource {
private String passwordDis;
/**
* 密匙
*/
private final static String Pkey ="1234565437892132";
@Override
public String getPassword(){ if(StringUtils.isNotBlank(passwordDis)){return passwordDis;}
String encPassword = super.getPassword();
if(null==encPassword){
return null;
}
log.info("数据库密码加解密,{"+encPassword+"}");
try{
// 密文解密,解密方法可以修改
String key = HexUtil.encodeHexStr(Pkey);
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes());
passwordDis = aes.decryptStr(encPassword, CharsetUtil.CHARSET_UTF_8);
return passwordDis;
}catch (Exception e){
log.error("数据库密码解密出错,{"+encPassword+"}");
log.error(LogUtil.e(e));
throw new AppException("数据库密码解密失败!", e);
}
}
}

3.初始化DataSource类

@Component
public class CommonBeanFactory {
@Bean(name = "dataSource", autowire = Autowire.NO, initMethod = "init")
@Primary
@ConfigurationProperties(ignoreUnknownFields = false,prefix="spring.datasource")
public DruidDataSource dataSource() {
DruidDataSource druidDataSource = new UmspscDataSource();
return druidDataSource;
}
}

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

构建密文

@Slf4j
public class Main { public static void main(String[] args) {
//明文
String content = "123456";
//密匙
String pkey = "1234565437892132";
log.info("密匙:" + pkey);
String key = HexUtil.encodeHexStr(pkey);
//构建
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key.getBytes()); //加密为16进制表示
String encryptHex = aes.encryptHex(content);
log.info("密文:" + encryptHex);
//解密为字符串
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
log.info("明文:" + decryptStr);
}
}

SpringBoot整合Druid,密码加密的更多相关文章

  1. SpringBoot使用Druid数据库加密链接完整方案

    网上的坑 springboot 使用 Druid 数据库加密链接方案,不建议采用网上的一篇文章<springboot 结合 Druid 加密数据库密码遇到的坑!>介绍的方式来进行加密链接实 ...

  2. SpringBoot整合Druid数据连接池

    SpringBoot整合Druid数据连接池 Druid是什么? Druid是Alibaba开源的的数据库连接池.Druid能够提供强大的监控和扩展功能. 在哪里下载druid maven中央仓库: ...

  3. 【SpringBoot | Druid】SpringBoot整合Druid

    SpringBoot整合Druid Druid是个十分强大的后端管理工具,具体的功能和用途请问阿里爸爸 1. 在pom.xml中导入包 <!-- alibaba 的druid数据库连接池 --& ...

  4. springboot整合druid、mybatis

    目的: 1.springboot配置数据库连接池druid 测试druid中url监控 2.springboot整合mybatis 测试查删案例 3.springboot整合pagehelper sp ...

  5. SpringBoot 整合 Shiro 密码登录与邮件验证码登录(多 Realm 认证)

    导入依赖(pom.xml)  <!--整合Shiro安全框架--> <dependency> <groupId>org.apache.shiro</group ...

  6. Druid密码加密

    pom里引用: <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring ...

  7. Springboot整合druid

    目录 Springboot整合druid application.yml DruidConfig 数据监控地址:http://localhost:8080/druid Springboot整合drui ...

  8. springboot整合druid和配置资源监控

    1.添加依赖,在maven repository中搜索 <dependency> <groupId>com.alibaba</groupId> <artifa ...

  9. 【SpringBoot笔记】SpringBoot整合Druid数据连接池

    废话少说,按SpringBoot的老套路来. [step1]:添加依赖 <!-- 数据库连接池 --> <dependency> <groupId>com.alib ...

随机推荐

  1. python路径引用r的含义

    input_file = r"C:\Users\Administrator\Desktop\python-master\csv\supplier_data.csv"#r代表不转义, ...

  2. Swagger添加文件上传测试

    先上对比图 图一无法选择文件,图二可以选择文件 图一 图二 添加过滤器 public class SwaggerFileUploadFilter : IOperationFilter { /// &l ...

  3. Altium Designer 19使用

    铺铜之后运行DRC检查弹出警告: Design contains shelved or modified (but not repoured)polygons. The result of DRC w ...

  4. HTML5 绘制阴影

    代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8 ...

  5. [AtCoder] Yahoo Programming Contest 2019

    [AtCoder] Yahoo Programming Contest 2019   很遗憾错过了一场 AtCoder .听说这场是涨分场呢,于是特意来补一下题. A - Anti-Adjacency ...

  6. NOSQL导图

  7. Hibernate入门核心配置文件和orm元数据配置文件详解

    框架是什么? 框架是用来提高开发效率的 封装了一些功能,我们需要使用这些功能时,调用即可,不用手动实现 所以框架可以理解为一个半成品的项目,只要懂得如何使用这些功能即可 Hibernate是完全面向对 ...

  8. create-react-app按需引入antd-mobile

    1.引入 react-app-rewired 并修改 package.json 里的启动配置: npm i react-app-rewired@2.0.2-next.0 // 需要安装低版本 否则np ...

  9. vue中使用canvas绘制签名

    不多说,上代码: <template>         <div class="sign-canvas">             <canvas   ...

  10. Struts和Hibernate的jar包

    这几天做了一个javaee关于struts框架和Hibernate框架的实践,实践内容倒是没什么,关键是找框架的配置花了许多时间 于是在这里把这两个框架的有关jar上传分享一下 链接: https:/ ...