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. 初学Java 九九乘法表

    public class MultiplicationTable { public static void main(String[] args) { System.out.println(" ...

  2. Go's Declaration Syntax

    Introduction Newcomers to Go wonder why the declaration syntax is different from the tradition estab ...

  3. Facebook的利润创下历史新高,不受最近的丑闻影响

    外媒:Facebook周三表示,其第一季度收入主要由广告支出,增长49%至120亿美元.净利润同比增长65%,创纪录的49亿美元. 尽管很多用户表示,他们在3月中旬发生的隐私丑闻导致他们删除了Face ...

  4. 存储过程如何传变量到like下

    存储过程中执行如下DDL语句create or replace procedure etl_test(v_com varchar2) is v_spname varchar2(40); com var ...

  5. 人生苦短_我用Python_dict(字典)_003

    # coding=utf-8 # 字典 {"key":"values"} 无序的 # dict 可以包含任何类型的数据 values可以是任何数据类型 key必 ...

  6. springmvc中拦截器配置格式

    对于springmvc,有两种方式配置拦截器. 一是实现HandlerInterceptor接口,如 public class MyInterceptor1 implements HandlerInt ...

  7. 前端开发工具-VsCode插件【个人开发常用】

     前端开发工具-VsCode插件[个人开发常用] Atom One Dark Theme-主题 Chinese (Simplified) Language Pack for Visual Studio ...

  8. IDEA不认识jstl

    解决方案:一.在pom.xml文件查看是否<packaging>的值是否是war  二:在jsp文件中加上这句话. <%@page isELIgnored="false&q ...

  9. fedora下编译运行java傻瓜入门级教程

    操作步骤: 编译运行环境的搭建 fedora下默认安装为GNU公司的JDK,如下所示 $java -version java version "1.7.0_25" OpenJDK ...

  10. 【MySQL】实现自增函数sequence

    前言 当前数据库为:mysql由于mysql和oracle不太一样,不支持直接的sequence,所以需要创建一张table来模拟sequence的功能,理由sql语句如下: 步骤 1.创建seque ...