springboot 使用druid 配置数据库密码加密
那么现在spring boot集成druid已经完成了。只差将密码加密并且写入配置文件即可。将password和publickey填入配置文件中即可完成。springboot 使用druid 配置数据库密码加密。1.添加DruidConfig配置类。1,找到druid的相关jar包。2.给配置文件中配置数据库。
·
springboot 使用druid 配置数据库密码加密
1.添加DruidConfig配置类
package org.jeecg.config;
import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* Druid配置类
* 1、可以监控数据库访问性能,内置了插件:StatFilter,能够详细统计SQL执行性能,用于线上分析数据库访问性能。
* 2、替换DBCP、C3P0数据库连接池,提供了一个高效稳定的扩展性能好的数据库连接池DruidDataSource。
* 3、数据库加密,DruidDriuiver和DruidDataSource都支持PasswordCallback。
* 4、SQL执行日志。
* 5、扩展JDBC,如果你要对JDBC层有编程的需求,可以通过Druid提供的Filter-Chain机制,很方便编写JDBC层的扩展插件
* Druid是一个阿里开源的数据库连接池框架,有可视化界面。
* http://localhost:8080/druid/login.html
* 可以查看sql访问速度等等很多信息,感觉还是比较有用的。集成进来试试
* @author maybe
*/
@Configuration
@Slf4j
public class DruidConfig {
//使用@value注入配置文件中信息
@Value("${spring.datasource.dynamic.datasource.master.url}")
private String dbUrl;
@Value("${spring.datasource.dynamic.datasource.master.username}")
private String username;
@Value("${spring.datasource.dynamic.datasource.master.password}")
private String password;
@Value("${spring.datasource.dynamic.datasource.master.public-key}")
private String publicKey;
@Value("${spring.datasource.dynamic.datasource.master.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.dynamic.druid.initial-size}")
private int initialSize;
@Value("${spring.datasource.dynamic.druid.min-idle}")
private int minIdle;
@Value("${spring.datasource.dynamic.druid.max-active}")
private int maxActive;
@Value("${spring.datasource.dynamic.druid.max-wait}")
private int maxWait;
@Value("${spring.datasource.dynamic.druid.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.dynamic.druid.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.dynamic.druid.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.dynamic.druid.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.dynamic.druid.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.dynamic.druid.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.dynamic.druid.filters}")
private String filters;
@Value("${spring.datasource.dynamic.druid.logSlowSql}")
private String logSlowSql;
@Value("${spring.datasource.dynamic.druid.connectionProperties}")
private String connectionProperties;
@Bean
public ServletRegistrationBean druidServlet() throws Exception {
ServletRegistrationBean reg = new ServletRegistrationBean();
reg.setServlet(new StatViewServlet());
reg.addUrlMappings("/druid/*");//配置访问URL
reg.addInitParameter("loginUsername", username); //配置用户名,这里使用数据库账号。
reg.addInitParameter("loginPassword", ConfigTools.decrypt(publicKey,password)); //配置用户名,这里使用数据库密码
reg.addInitParameter("logSlowSql", logSlowSql); //是否启用慢sql
return reg;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); //配置那些资源不被拦截
filterRegistrationBean.addInitParameter("profileEnable", "true");
return filterRegistrationBean;
}
/**
* 这个应该是数据库连接池配置
* @return
* @throws Exception
*/
@Bean
public DataSource druidDataSource() throws Exception {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(dbUrl);
datasource.setUsername(username);
datasource.setPassword(ConfigTools.decrypt(publicKey,password));
datasource.setDriverClassName(driverClassName);
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setConnectionProperties(connectionProperties);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
log.error("druid configuration initialization filter:{}",e);
}
return datasource;
}
}
2.给配置文件中配置数据库
spring
datasource:
druid:
stat-view-servlet:
enabled: true
loginUsername: admin
loginPassword: 123456
allow:
web-stat-filter:
enabled: true
dynamic:
druid: # 全局druid参数,绝大部分值和默认保持一致。(现已支持的参数如下,不清楚含义不要乱设置)
# 连接池的配置信息
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
logSlowSql: true
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j,config
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
datasource:
master:
url: jdbc:mysql://127.0.0.1:3306/jeecg_boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false
username: root
password: U9FSu3rAgada8zV0VGTVEeAKc3Ccxgzbz9nuC4AWKzrxdZIJLvdnCjy9D913jI5sHxlzK6HfdR5+cEbPhM8v9A==
public-key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALR6ubZQXlaJQIVTxgqxKMdhKjhwTcsQDQq8Rvxa0G/gnZHCMxSZMtiWD8p7L4pHYWys99sOzfIBiP14LZkzNMUCAwEAAQ==
driver-class-name: com.mysql.jdbc.Driver
# 多数据源配置
#multi-datasource1:
#url: jdbc:mysql://localhost:3306/jeecg-boot2?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
#username: root
#password: root
#driver-class-name: com.mysql.jdbc.Driver
那么现在spring boot集成druid已经完成了。只差将密码加密并且写入配置文件即可。
druid进行数据库密码加密:
1,找到druid的相关jar包
这里我是用的是
2,进行加密
进行cmd找到该jar包所在目录,执行:
java -cp druid-1.1.17.jar com.alibaba.druid.filter.config.ConfigTools your-password
得到:
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtHq5tlBeVolAhVPGCrEox
2EqOHBNyxANCrxG/FrQb+CdkcIzFJky2JYPynsvikdhbKz32w7N8gGI/XgtmTM0xQIDAQABAkAhnF1rm
4oGGad/bPuL0DRk9S50tk12jLWxEyExLAR3YRAYZTDREv4XIM4paB1Q3NLgOnYXWBpr5/SwfFMq4BqdA
iEA5gd7XORlZDq19nWyW05hTyqrsct/oVCXf6wi2GVB5HsCIQDI2xu/4uR55WcBDwQMHKSdId07XhtK1
ZeU5asWAEEnvwIgbUN8i6FJsBRbPpilgc13V31zVN7O3zdC3zhKzPM8G2UCIQCj+waHvXSs9BYJLjour
T/TrW8R5f3ANetgO7VW6/RFvwIhAIfRioDqbwOLI9c4ppneBIDw6Hk6lzLonX3eU9EGBznx
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALR6ubZQXlaJQIVTxgqxKMdhKjhwTcsQDQq8Rv
xa0G/gnZHCMxSZMtiWD8p7L4pHYWys99sOzfIBiP14LZkzNMUCAwEAAQ==
password:U9FSu3rAgada8zV0VGTVEeAKc3Ccxgzbz9nuC4AWKzrxdZIJLvdnCjy9D913jI5sHxlzK6H
fdR5+cEbPhM8v9A==
1,privateKey 私钥,进行加密时的key
2,publickey 公钥,进行解密时的key
3,password 加密后的密码
将password和publickey填入配置文件中即可完成。
更多推荐
已为社区贡献4条内容
所有评论(0)