mysql数据库druid密码加密_SpringBoot使用Druid数据库密码加密

本文介绍了如何在SpringBoot项目中使用Druid数据源进行MySQL数据库密码加密,包括使用Druid工具类ConfigTools加密密码,配置加密后的数据源参数,并在application.properties中设置解密相关属性,以及详细的数据源配置注入步骤。
摘要由CSDN通过智能技术生成

java -cp druid-1.0.28.jar com.alibaba.druid.filter.config.ConfigTools pcds123123456

2、使用 Durid 的工具类 ConfigTools 验证加密字符串

@Test

public void db_decrypt_test() throws Exception {

String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJggkaRJ+bqLMF6pefubEDLViboxYKGTdGe+78DziIta8Nv8crOA83M0tFG8y8CqHcFYIbG89q9zcnNvL+E2/CECAwEAAQ==";

String password = "AgDRyKJ81Ku3o0HSyalDgCTtGsWcKz3fC0iM5pLur2QJnIF+fKWKFZ6c6e36M06tF2uCadvS/EodWxmRDWwvIA==";

System.out.println(ConfigTools.decrypt(publicKey, password));

}

3、application.properties中的数据源配置

# mysql config

spring.datasource.name=pcdsdata

spring.datasource.url=jdbc:mysql://192.168.1.1/mydb

spring.datasource.username=root

spring.datasource.password=AgDRyKJ81Ku3o0HSyalDgCTtGsWcKz3fC0iM5pLur2QJnIF+fKWKFZ6c6e36M06tF2uCadvS/EodWxmRDWwvIA==

# druid datasource config

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.maxActive=20

spring.datasource.initialSize=1

spring.datasource.minIdle=1

spring.datasource.maxWait=60000

spring.datasource.timeBetweenEvictionRunsMillis=60000

spring.datasource.minEvictableIdleTimeMillis=300000

spring.datasource.validationQuery=select 1 from dual

spring.datasource.testWhileIdle=true

spring.datasource.testOnBorrow=false

spring.datasource.testOnReturn=false

spring.datasource.poolPreparedStatements=true

spring.datasource.maxOpenPreparedStatements=20

spring.datasource.maxPoolPreparedStatementPerConnectionSize=20

spring.datasource.filters=config,stat,wall,log4j

spring.datasource.publicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJggkaRJ+bqLMF6pefubEDLViboxYKGTdGe+78DziIta8Nv8crOA83M0tFG8y8CqHcFYIbG89q9zcnNvL+E2/CECAwEAAQ==

spring.datasource.connectionProperties=config.decrypt=true;config.decrypt.key=${spring.datasource.publicKey}

4、配置注入数据源Bean

@Configuration

@ConfigurationProperties(prefix = "spring.datasource")

public class DataSourceConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceConfig.class);

private String url;

private String driverClassName;

private String username;

private String password;

private Integer initialSize;

private Integer minIdle;

private Integer maxActive;

private Integer maxWait;

private Integer timeBetweenEvictionRunsMillis;

private Integer minEvictableIdleTimeMillis;

private String validationQuery;

private Boolean testWhileIdle;

private Boolean testOnBorrow;

private Boolean testOnReturn;

private Boolean poolPreparedStatements;

private Integer maxOpenPreparedStatements;

private Integer maxPoolPreparedStatementPerConnectionSize;

private String filters;

private String publicKey;

private String connectionProperties;

@Primary

@Bean(name = "dataSource")

public DataSource dataSource() {

DruidDataSource datasource = new DruidDataSource();

datasource.setUrl(url);

datasource.setUsername(username);

datasource.setPassword(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.setPoolPreparedStatements(poolPreparedStatements);

datasource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);

datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

datasource.setConnectionProperties(connectionProperties);

try {

datasource.setFilters(filters);

} catch (SQLException e) {

LOGGER.error("druid configuration initialization filter", e);

}

return datasource;

}

/**

* 配置mybatis使用的sqlSessionFactory

* @return

*/

@Bean

@ConfigurationProperties(prefix = "mybatis")

public SqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) {

SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

sqlSessionFactoryBean.setDataSource(dataSource);

return sqlSessionFactoryBean;

}

/**

* 配置JdbcTemplate

* @param dataSource

* @return

*/

@Bean(name = "jdbcTemplate")

public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {

return new JdbcTemplate(dataSource);

}

/**

* 配置@Transactional注解事物

* @return

*/

@Bean

public PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getDriverClassName() {

return driverClassName;

}

public void setDriverClassName(String driverClassName) {

this.driverClassName = driverClassName;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public Integer getInitialSize() {

return initialSize;

}

public void setInitialSize(Integer initialSize) {

this.initialSize = initialSize;

}

public Integer getMinIdle() {

return minIdle;

}

public void setMinIdle(Integer minIdle) {

this.minIdle = minIdle;

}

public Integer getMaxActive() {

return maxActive;

}

public void setMaxActive(Integer maxActive) {

this.maxActive = maxActive;

}

public Integer getMaxWait() {

return maxWait;

}

public void setMaxWait(Integer maxWait) {

this.maxWait = maxWait;

}

public Integer getTimeBetweenEvictionRunsMillis() {

return timeBetweenEvictionRunsMillis;

}

public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) {

this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;

}

public Integer getMinEvictableIdleTimeMillis() {

return minEvictableIdleTimeMillis;

}

public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) {

this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;

}

public String getValidationQuery() {

return validationQuery;

}

public void setValidationQuery(String validationQuery) {

this.validationQuery = validationQuery;

}

public Boolean getTestWhileIdle() {

return testWhileIdle;

}

public void setTestWhileIdle(Boolean testWhileIdle) {

this.testWhileIdle = testWhileIdle;

}

public Boolean getTestOnBorrow() {

return testOnBorrow;

}

public void setTestOnBorrow(Boolean testOnBorrow) {

this.testOnBorrow = testOnBorrow;

}

public Boolean getTestOnReturn() {

return testOnReturn;

}

public void setTestOnReturn(Boolean testOnReturn) {

this.testOnReturn = testOnReturn;

}

public Boolean getPoolPreparedStatements() {

return poolPreparedStatements;

}

public void setPoolPreparedStatements(Boolean poolPreparedStatements) {

this.poolPreparedStatements = poolPreparedStatements;

}

public Integer getMaxOpenPreparedStatements() {

return maxOpenPreparedStatements;

}

public void setMaxOpenPreparedStatements(Integer maxOpenPreparedStatements) {

this.maxOpenPreparedStatements = maxOpenPreparedStatements;

}

public Integer getMaxPoolPreparedStatementPerConnectionSize() {

return maxPoolPreparedStatementPerConnectionSize;

}

public void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) {

this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;

}

public String getFilters() {

return filters;

}

public void setFilters(String filters) {

this.filters = filters;

}

public String getPublicKey() {

return publicKey;

}

public void setPublicKey(String publicKey) {

this.publicKey = publicKey;

}

public String getConnectionProperties() {

return connectionProperties;

}

public void setConnectionProperties(String connectionProperties) {

this.connectionProperties = connectionProperties;

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2019-03-06 23:28

浏览 882

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值