SpringBoot集成Druid和数据库密码加密
Druid是阿里开发的数据库连接池,通过简单的配置,可以实现数据库的连接,性能特别强大,可以在页面访问,包括监控数据库性能参数,慢SQL统计,当然还包括数据库连接等。
今天主要记录一下SpringBoot集成Druid和数据库密码加密。
一、SpringBoot集成Druid
1、增加Druid依赖
<!-- 阿里数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.11</version></dependency>
2、配置yml
# Spring配置
spring:# 数据源配置datasource:#连接池的配置信息type: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: oracle.jdbc.OracleDriverurl: jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521/ORCLPDB1username: xxxxpassword: xxxx
3、Druid配置
@Configuration
@MapperScan(basePackages = DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DataSourceConfig {static final String PACKAGE = "com.xxx.xxx.mapper";static final String MAPPER_LOCATION = "classpath:mapper/**.xml";@Value("${spring.datasource.druid.url}")private String url;@Value("${spring.datasource.druid.username}")private String user;@Value("${spring.datasource.druid.password}")private String password;@Value("${spring.datasource.druid.driver-class-name}")private String driverClass;@Bean(name = "masterDataSource")@Primarypublic DataSource masterDataSource() throws SQLException {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driverClass);dataSource.setUrl(url);dataSource.setUsername(user);dataSource.setPassword(password);return dataSource;}@Bean@Primarypublic DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean@Primarypublic SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(masterDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}}
4、代码结构
二、配置数据库密码加密
1、加密,首先需要生成自己的私钥和公钥,然后对密码进行加密,得到的公钥和密码就是我们需要配置的
public static void main(String[] args) throws Exception{//密码明文String password = "123";System.out.println("明文密码: " + password);
// // 获取密钥String[] keyPair = ConfigTools.genKeyPair(512);//私钥String privateKey = keyPair[0];
// String privateKey = "MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAoJVSCPpJiAwR9JPpBqK4YQLeLZfS2UUvFwp7XwYVhNhqzguFH6EL7oUjIahrSYlwDRTAZozHF6EcNyj5YcM90QIDAQABAkBcCNiojpJAJ/LOg0tF41LbPuKJrP9KSS2Q/g/xSTJiHSAUxH/iSUMtd6xxTZ9sm3Wgul12fIqmFWOv+fPx7gdFAiEA7BzX3qir3hVfASTExJ1s4hsw3LjY71s6evESR/i/IfcCIQCuG+HPpxaet1FDKo9dWUaZIoF6WEFr/bGjEhMYTIwsdwIhALL2YcDIxAw+0pXBUstcL01qIq0KBpPV6AuLcbnPlr+dAiEAl4m7C7JhVLk3aF9VsqjucoB+8053epevkcA8kGynoFcCIQDhiTKseiMkkjIvxtFCYWbS9ZQNBKjuGBspS4sRJ4IOhg==";//公钥String publicKey = keyPair[1];
// String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKCVUgj6SYgMEfST6QaiuGEC3i2X0tlFLxcKe18GFYTYas4LhR+hC+6FIyGoa0mJcA0UwGaMxxehHDco+WHDPdECAwEAAQ==";System.out.println("privateKey(私钥):" + privateKey);System.out.println("publicKey(公钥):" + publicKey);//用私钥加密后的密文String encryptPassword = ConfigTools.encrypt(privateKey, password);System.out.println("用私钥加密后的密文:" + encryptPassword);String decryptPassword = ConfigTools.decrypt(publicKey, encryptPassword);System.out.println("解密后:" + decryptPassword);}
如果是多数据源需要保存私钥和公钥,对其他密码进行加密。上面代码就是我执行完一次之后,第二次执行加密,用的就是我第一次执行的私钥和公钥,并且分别保存好加密后的密码和公钥。
2、配置yml
# Spring配置
spring:# 数据源配置datasource:#连接池的配置信息type: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: oracle.jdbc.OracleDriverurl: jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521/ORCLPDB1username: xxxxpassword: IUQ7wHt4XMhYxojPhXM5epf9ZJ4dBjQguY/fJKhcvC39yAZIwGfeFhnZ+kXM1NrE+Fe8cXvCrQFuaT6LYyqNkQ==
上面password就是我们加密之后的密码。
3、Druid配置修改
@Bean(name = "masterDataSource")@Primarypublic DataSource masterDataSource() throws SQLException {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driverClass);dataSource.setUrl(url);dataSource.setUsername(user);dataSource.setPassword(password);// 增加配置,配合进行密码解密Properties properties = new Properties();properties.setProperty("config.decrypt","true");// 此处配置的是公钥properties.setProperty("config.decrypt.key","MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKCVUgj6SYgMEfST6QaiuGEC3i2X0tlFLxcKe18GFYTYas4LhR+hC+6FIyGoa0mJcA0UwGaMxxehHDco+WHDPdECAwEAAQ==");dataSource.setConnectProperties(properties);dataSource.setFilters("config");return dataSource;}
三、多数据源加密
1、代码结构
2、yml配置
3、Druid配置
主数据源
@Configuration
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {static final String PACKAGE = "com.xxxx.xxxx.mapper.master";static final String MAPPER_LOCATION = "classpath:mapper/master/**.xml";@Value("${spring.datasource.druid.master.url}")private String url;@Value("${spring.datasource.druid.master.username}")private String user;@Value("${spring.datasource.druid.master.password}")private String password;@Value("${spring.datasource.druid.master.driver-class-name}")private String driverClass;@Bean(name = "masterDataSource")@Primarypublic DataSource masterDataSource() throws SQLException {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driverClass);dataSource.setUrl(url);dataSource.setUsername(user);dataSource.setPassword(password);/*** privateKey(私钥):MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAoJVSCPpJiAwR9JPpBqK4YQLeLZfS2UUvFwp7XwYVhNhqzguFH6EL7oUjIahrSYlwDRTAZozHF6EcNyj5YcM90QIDAQABAkBcCNiojpJAJ/LOg0tF41LbPuKJrP9KSS2Q/g/xSTJiHSAUxH/iSUMtd6xxTZ9sm3Wgul12fIqmFWOv+fPx7gdFAiEA7BzX3qir3hVfASTExJ1s4hsw3LjY71s6evESR/i/IfcCIQCuG+HPpxaet1FDKo9dWUaZIoF6WEFr/bGjEhMYTIwsdwIhALL2YcDIxAw+0pXBUstcL01qIq0KBpPV6AuLcbnPlr+dAiEAl4m7C7JhVLk3aF9VsqjucoB+8053epevkcA8kGynoFcCIQDhiTKseiMkkjIvxtFCYWbS9ZQNBKjuGBspS4sRJ4IOhg==*/Properties properties = new Properties();properties.setProperty("config.decrypt","true");properties.setProperty("config.decrypt.key","MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKCVUgj6SYgMEfST6QaiuGEC3i2X0tlFLxcKe18GFYTYas4LhR+hC+6FIyGoa0mJcA0UwGaMxxehHDco+WHDPdECAwEAAQ==");dataSource.setConnectProperties(properties);dataSource.setFilters("config");return dataSource;}@Bean@Primarypublic DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean@Primarypublic SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(masterDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}}
次级数据源
@Configuration
@MapperScan(basePackages = SecondaryDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class SecondaryDataSourceConfig {static final String PACKAGE = "com.xxxx.xxxx.mapper.secondary";static final String MAPPER_LOCATION = "classpath:mapper/secondary/**.xml";@Value("${spring.datasource.druid.secondary.url}")private String url;@Value("${spring.datasource.druid.secondary.username}")private String user;@Value("${spring.datasource.druid.secondary.password}")private String password;@Value("${spring.datasource.druid.secondary.driver-class-name}")private String driverClass;@Bean(name = "secondaryDataSource")public DataSource secondaryDataSource() throws SQLException {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driverClass);dataSource.setUrl(url);dataSource.setUsername(user);dataSource.setPassword(password);/*** privateKey(私钥):MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAoJVSCPpJiAwR9JPpBqK4YQLeLZfS2UUvFwp7XwYVhNhqzguFH6EL7oUjIahrSYlwDRTAZozHF6EcNyj5YcM90QIDAQABAkBcCNiojpJAJ/LOg0tF41LbPuKJrP9KSS2Q/g/xSTJiHSAUxH/iSUMtd6xxTZ9sm3Wgul12fIqmFWOv+fPx7gdFAiEA7BzX3qir3hVfASTExJ1s4hsw3LjY71s6evESR/i/IfcCIQCuG+HPpxaet1FDKo9dWUaZIoF6WEFr/bGjEhMYTIwsdwIhALL2YcDIxAw+0pXBUstcL01qIq0KBpPV6AuLcbnPlr+dAiEAl4m7C7JhVLk3aF9VsqjucoB+8053epevkcA8kGynoFcCIQDhiTKseiMkkjIvxtFCYWbS9ZQNBKjuGBspS4sRJ4IOhg==*/Properties properties = new Properties();properties.setProperty("config.decrypt","true");properties.setProperty("config.decrypt.key","MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKCVUgj6SYgMEfST6QaiuGEC3i2X0tlFLxcKe18GFYTYas4LhR+hC+6FIyGoa0mJcA0UwGaMxxehHDco+WHDPdECAwEAAQ==");dataSource.setConnectProperties(properties);dataSource.setFilters("config");return dataSource;}@Beanpublic DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Beanpublic SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource secondaryDataSource)throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(secondaryDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(SecondaryDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}}
两个数据源大体配置相同。
四、这样毕竟公钥在代码中存在,还是不安全的,我们可以把在启动程序参数中。
程序参数: --publicKey=你的公钥。
在Linux的启动程序参数配置,java -jar xxxx.jar --publicKey=你的公钥
这样我们多数据源数据库密码加密就完成了。
SpringBoot集成Druid和数据库密码加密相关推荐
- 集成druid实现数据库密码加密功能
数据库密码直接写在配置中,对运维安全来说,是一个很大的挑战.可以使用Druid为此提供一种数据库密码加密的手段ConfigFilter. 目录 1.执行命令加密数据库密码 2.配置数据源,提示Drui ...
- SpringBoot 集成 druid 监控数据库报错 Failed to bind properties under ‘xxxx‘ to javax.sql.DataSource 解决(含配置源码)
What Druid是一个JDBC组件,它包括三部分: • DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. • DruidDataSource 高效可管 ...
- druid连接池配置数据库密码加密
druid配置数据库密码加密后,可以把密码放在配置文件里,或本地其他文件.远程服务器等三种地方,这里只讲第一种方法. 1.密码加密:cd到druid包所在文件夹打开命令行,输入 java -cp dr ...
- java配置文件中数据库密码加密
最近,有位读者私信我说,他们公司的项目中配置的数据库密码没有加密,编译打包后的项目被人反编译了,从项目中成功获取到数据库的账号和密码,进一步登录数据库获取了相关的数据,并对数据库进行了破坏. 虽然这次 ...
- springboot+druid+dynamic-datasource+mysql数据库密码加密
背景 生产环境中, 希望将数据库密码加密, 甚至用户名加密.druid自带了数据库密码加密功能. springboot+druid-starter实现密码加密 获取公钥和加密密码.这里使用druid- ...
- druid ssh加密 java mysql_springboot 整合druid数据库密码加密功能的实现代码
在之前给大家介绍过Springboot Druid 自定义加密数据库密码的几种方案,感兴趣的朋友可以点击查看下,今天通过本文给大家介绍springboot 整合druid数据库密码加密功能,具体内容如 ...
- SpringBoot配置文件敏感信息加密,springboot配置文件数据库密码加密jasypt
使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些.打开application.properties或application.yml,比如mysq ...
- mysql数据库druid密码加密_Druid数据库密码加密
背景 数据库密码直接写在配置中,对安全来说,是一个很大的挑战.Druid为此提供一种数据库密码加密的手段ConfigFilter. druid版本为1.1.18. 加密 1.项目中引入依赖 com.a ...
- SpringBoot配置文件数据库密码加密
引言 需求:springboot的配置文件中,把连接数据库的密码加密,使之不是以明文存储 步骤 导入Maven坐标 <dependency><groupId>com.githu ...
最新文章
- 深度学习必须掌握的 13 种概率分布
- oracle sql判断值为空,Oracle,sqlserver的空值(null)判断
- 验证Xcode真伪的方法,来自苹果官网
- 贝佐斯成功拉下马斯克,NASA把SpaceX的独家登月合同暂停了
- php 接口测压,PHP API接口测试小工具
- strcpy_s与strcpy对照
- 198. House Robber
- flink的分桶策略BucketAssigner与hive的分区的对应关系
- QuickBI助你成为分析师-邮件定时推送
- Android - 获取字符串长度的宽度
- 电脑多功能图像工具箱V1.9-79套功能非常实用!
- java 设置文本框文字居中_div居中 边框设置 文字行高设置
- oracle toda和pl/sql匪夷所思的差异
- 三极管的基础知识(下)②
- 【项目实战】Python基于孤立森林算法(IsolationForest)实现数据异常值检测项目实战
- 了解Nape 2d物理引擎 第一天
- PHP与SEO,应用curl及正则获取搜狗搜索相关关键词
- 神经网络模型 基本概念 一文看懂
- CSP 2022 提高组普及组总结
- 携手华为,瑞金医院病理科为健康数字化保驾护航
热门文章
- ucsd计算机排名,加州大学圣地亚哥分校专业排名一览及最强专业推荐(QS世界大学排名)...
- Glyphs for Mac 2.6.5 — 字体设计工具
- 我的新书——《PHP程序员面试笔试宝典》
- Ls -l命令linux,ls命令简介
- 如何运用计算机教学教学的收获,计算机教学中行动感悟法的应用
- 金蝶K3生产数据管理模块术语表
- 有限元与深度学习结合求解泊松方程-Petrov
- 上线网站详细介绍(服务器购买-域名申请-SSL证书申请)
- r720服务器怎么查看硬盘性能,r720服务器如何看配置
- 真正的能理解CSS中的line-height,height与line-height