Commit 517ca0e5 by songxiang

数据库密码加密处理

parent 7335b717
......@@ -10,6 +10,23 @@ spring:
loadbalancer:
retry:
enabled: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
initialSize: 2
minIdle: 5
maxActive: 50
maxWait: 60000
timeBetweenEvictionRunsMillis: 3000
minEvictableIdleTimeMillis: 3600000
validationQuery: SELECT 'x' FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
connectionPropertiesNew: config.decrypt=true;config.decrypt.key=${spring.datasource.publicKey}
filtersNew: config
metrics:
influx:
uri: http://192.168.83.241:8086/write
......
......@@ -10,6 +10,23 @@ spring:
loadbalancer:
retry:
enabled: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
initialSize: 2
minIdle: 5
maxActive: 50
maxWait: 60000
timeBetweenEvictionRunsMillis: 3000
minEvictableIdleTimeMillis: 3600000
validationQuery: SELECT 'x' FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
connectionPropertiesNew: config.decrypt=true;config.decrypt.key=${spring.datasource.publicKey}
filtersNew: config
metrics:
influx:
uri: http://192.168.83.241:8086/write
......
......@@ -16,6 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
......@@ -25,139 +26,282 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan(basePackages = DataSourceConfig.PACKAGE,sqlSessionFactoryRef = "sqlSessionFactory")
@ConfigurationProperties(prefix = "spring.datasource")
@MapperScan(basePackages = DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {
private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
static final String PACKAGE = "com.pcloud.common.core.dao";
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.type}")
private String dbType;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.filters}")
private String filters;
@Value("${spring.datasource.connectionProperties}")
private String connectionProperties;
@Value("${mybatis.mapper-locations}")
private String mapperLocations;
@Value("${mybatis.type-aliases-package}")
private String typeAliasesPackage;
// @Value("${spring.datasource.useGlobalDataSourceStat}")
// private boolean useGlobalDataSourceStat;
//
// @Value("${spring.datasource.druidLoginName}")
// private String druidLoginName;
//
// @Value("${spring.datasource.druidPassword}")
// private String druidPassword;
@Bean(name="druidDataSource")
@Primary //不要漏了这
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
try {
datasource.setUrl(this.dbUrl);
datasource.setDbType(dbType);
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.setFilters(filters);
datasource.setConnectionProperties(connectionProperties);
// datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
ArrayList<String> arr = new ArrayList<>();
arr.add("set names utf8mb4;");
datasource.setConnectionInitSqls(arr);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
return datasource;
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager transactionManager() {
logger.info("【DataSource】初始化TransactionManager事务管理器,<START>");
return new DataSourceTransactionManager(dataSource());
}
@Bean(name = "sqlSessionFactory")
@Autowired
public SqlSessionFactory sqlSessionFactory(ExecutorInterceptor interceptor) throws Exception {
logger.info("【DataSource】初始化SqlSessionFactory,<START>");
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
sessionFactoryBean.setVfs(SpringBootVFS.class);
sessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
sessionFactoryBean.setPlugins(new Interceptor[]{interceptor});
return sessionFactoryBean.getObject();
}
@Bean(name = "sqlSessionTemplate")
@Autowired
public SqlSessionTemplate sqlSessionTemplate(ExecutorInterceptor interceptor) throws Exception {
logger.info("【DataSource】初始化SqlSessionTemplate,<START>");
final SqlSessionTemplate sessionFactoryBean = new SqlSessionTemplate(sqlSessionFactory(interceptor));
return sessionFactoryBean;
}
private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
static final String PACKAGE = "com.pcloud.common.core.dao";
private String url;
private String type;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
private int maxActive;
private int maxWait;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private String filtersNew;
private String connectionPropertiesNew;
@Value("${mybatis.mapper-locations}")
private String mapperLocations;
@Value("${mybatis.type-aliases-package}")
private String typeAliasesPackage;
// @Value("${spring.datasource.useGlobalDataSourceStat}")
// private boolean useGlobalDataSourceStat;
//
// @Value("${spring.datasource.druidLoginName}")
// private String druidLoginName;
//
// @Value("${spring.datasource.druidPassword}")
// private String druidPassword;
@Bean(name = "druidDataSource")
@Primary // 不要漏了这
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
try {
datasource.setUrl(url);
datasource.setDbType(type);
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.setFilters(filtersNew);
datasource.setConnectionProperties(connectionPropertiesNew);
// datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
ArrayList<String> arr = new ArrayList<>();
arr.add("set names utf8mb4;");
datasource.setConnectionInitSqls(arr);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
return datasource;
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager transactionManager() {
logger.info("【DataSource】初始化TransactionManager事务管理器,<START>");
return new DataSourceTransactionManager(dataSource());
}
@Bean(name = "sqlSessionFactory")
@Autowired
public SqlSessionFactory sqlSessionFactory(ExecutorInterceptor interceptor) throws Exception {
logger.info("【DataSource】初始化SqlSessionFactory,<START>");
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
sessionFactoryBean.setVfs(SpringBootVFS.class);
sessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
sessionFactoryBean.setPlugins(new Interceptor[] { interceptor });
return sessionFactoryBean.getObject();
}
@Bean(name = "sqlSessionTemplate")
@Autowired
public SqlSessionTemplate sqlSessionTemplate(ExecutorInterceptor interceptor) throws Exception {
logger.info("【DataSource】初始化SqlSessionTemplate,<START>");
final SqlSessionTemplate sessionFactoryBean = new SqlSessionTemplate(sqlSessionFactory(interceptor));
return sessionFactoryBean;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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 String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public int getInitialSize() {
return initialSize;
}
public void setInitialSize(int initialSize) {
this.initialSize = initialSize;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public int getMaxWait() {
return maxWait;
}
public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
}
public int getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public int getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public String getValidationQuery() {
return validationQuery;
}
public void setValidationQuery(String validationQuery) {
this.validationQuery = validationQuery;
}
public boolean isTestWhileIdle() {
return testWhileIdle;
}
public void setTestWhileIdle(boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
public boolean isTestOnReturn() {
return testOnReturn;
}
public void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
public boolean isPoolPreparedStatements() {
return poolPreparedStatements;
}
public void setPoolPreparedStatements(boolean poolPreparedStatements) {
this.poolPreparedStatements = poolPreparedStatements;
}
public String getFiltersNew() {
return filtersNew;
}
public void setFiltersNew(String filtersNew) {
this.filtersNew = filtersNew;
}
public String getConnectionPropertiesNew() {
return connectionPropertiesNew;
}
public void setConnectionPropertiesNew(String connectionPropertiesNew) {
this.connectionPropertiesNew = connectionPropertiesNew;
}
public String getMapperLocations() {
return mapperLocations;
}
public void setMapperLocations(String mapperLocations) {
this.mapperLocations = mapperLocations;
}
public String getTypeAliasesPackage() {
return typeAliasesPackage;
}
public void setTypeAliasesPackage(String typeAliasesPackage) {
this.typeAliasesPackage = typeAliasesPackage;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment