Commit 517ca0e5 by songxiang

数据库密码加密处理

parent 7335b717
...@@ -10,6 +10,23 @@ spring: ...@@ -10,6 +10,23 @@ spring:
loadbalancer: loadbalancer:
retry: retry:
enabled: true 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: metrics:
influx: influx:
uri: http://192.168.83.241:8086/write uri: http://192.168.83.241:8086/write
......
...@@ -10,6 +10,23 @@ spring: ...@@ -10,6 +10,23 @@ spring:
loadbalancer: loadbalancer:
retry: retry:
enabled: true 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: metrics:
influx: influx:
uri: http://192.168.83.241:8086/write uri: http://192.168.83.241:8086/write
......
...@@ -16,6 +16,7 @@ import org.slf4j.Logger; ...@@ -16,6 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
...@@ -25,139 +26,282 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager; ...@@ -25,139 +26,282 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
@Configuration @Configuration
@MapperScan(basePackages = DataSourceConfig.PACKAGE,sqlSessionFactoryRef = "sqlSessionFactory") @ConfigurationProperties(prefix = "spring.datasource")
@MapperScan(basePackages = DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig { public class DataSourceConfig {
private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class); private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
static final String PACKAGE = "com.pcloud.common.core.dao"; static final String PACKAGE = "com.pcloud.common.core.dao";
@Value("${spring.datasource.url}") private String url;
private String dbUrl;
private String type;
@Value("${spring.datasource.type}")
private String dbType; private String username;
@Value("${spring.datasource.username}") private String password;
private String username;
private String driverClassName;
@Value("${spring.datasource.password}")
private String password; private int initialSize;
@Value("${spring.datasource.driver-class-name}") private int minIdle;
private String driverClassName;
private int maxActive;
@Value("${spring.datasource.initialSize}")
private int initialSize; private int maxWait;
@Value("${spring.datasource.minIdle}") private int timeBetweenEvictionRunsMillis;
private int minIdle;
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.maxActive}")
private int maxActive; private String validationQuery;
@Value("${spring.datasource.maxWait}") private boolean testWhileIdle;
private int maxWait;
private boolean testOnBorrow;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis; private boolean testOnReturn;
@Value("${spring.datasource.minEvictableIdleTimeMillis}") private boolean poolPreparedStatements;
private int minEvictableIdleTimeMillis;
private String filtersNew;
@Value("${spring.datasource.validationQuery}")
private String validationQuery; private String connectionPropertiesNew;
@Value("${spring.datasource.testWhileIdle}") @Value("${mybatis.mapper-locations}")
private boolean testWhileIdle; private String mapperLocations;
@Value("${spring.datasource.testOnBorrow}") @Value("${mybatis.type-aliases-package}")
private boolean testOnBorrow; private String typeAliasesPackage;
@Value("${spring.datasource.testOnReturn}") // @Value("${spring.datasource.useGlobalDataSourceStat}")
private boolean testOnReturn; // private boolean useGlobalDataSourceStat;
//
@Value("${spring.datasource.poolPreparedStatements}") // @Value("${spring.datasource.druidLoginName}")
private boolean poolPreparedStatements; // private String druidLoginName;
//
@Value("${spring.datasource.filters}") // @Value("${spring.datasource.druidPassword}")
private String filters; // private String druidPassword;
@Value("${spring.datasource.connectionProperties}") @Bean(name = "druidDataSource")
private String connectionProperties; @Primary // 不要漏了这
public DataSource dataSource() {
@Value("${mybatis.mapper-locations}") DruidDataSource datasource = new DruidDataSource();
private String mapperLocations; try {
datasource.setUrl(url);
@Value("${mybatis.type-aliases-package}") datasource.setDbType(type);
private String typeAliasesPackage; datasource.setUsername(username);
datasource.setPassword(password);
// @Value("${spring.datasource.useGlobalDataSourceStat}") datasource.setDriverClassName(driverClassName);
// private boolean useGlobalDataSourceStat; datasource.setInitialSize(initialSize);
// datasource.setMinIdle(minIdle);
// @Value("${spring.datasource.druidLoginName}") datasource.setMaxActive(maxActive);
// private String druidLoginName; datasource.setMaxWait(maxWait);
// datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
// @Value("${spring.datasource.druidPassword}") datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
// private String druidPassword; datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
@Bean(name="druidDataSource") datasource.setTestOnBorrow(testOnBorrow);
@Primary //不要漏了这 datasource.setTestOnReturn(testOnReturn);
public DataSource dataSource(){ datasource.setPoolPreparedStatements(poolPreparedStatements);
DruidDataSource datasource = new DruidDataSource(); datasource.setFilters(filtersNew);
try { datasource.setConnectionProperties(connectionPropertiesNew);
datasource.setUrl(this.dbUrl); // datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
datasource.setDbType(dbType); ArrayList<String> arr = new ArrayList<>();
datasource.setUsername(username); arr.add("set names utf8mb4;");
datasource.setPassword(password); datasource.setConnectionInitSqls(arr);
datasource.setDriverClassName(driverClassName); } catch (SQLException e) {
datasource.setInitialSize(initialSize); logger.error("druid configuration initialization filter", e);
datasource.setMinIdle(minIdle); }
datasource.setMaxActive(maxActive); return datasource;
datasource.setMaxWait(maxWait); }
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); @Bean(name = "transactionManager")
datasource.setValidationQuery(validationQuery); public DataSourceTransactionManager transactionManager() {
datasource.setTestWhileIdle(testWhileIdle); logger.info("【DataSource】初始化TransactionManager事务管理器,<START>");
datasource.setTestOnBorrow(testOnBorrow); return new DataSourceTransactionManager(dataSource());
datasource.setTestOnReturn(testOnReturn); }
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setFilters(filters); @Bean(name = "sqlSessionFactory")
datasource.setConnectionProperties(connectionProperties); @Autowired
// datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat); public SqlSessionFactory sqlSessionFactory(ExecutorInterceptor interceptor) throws Exception {
ArrayList<String> arr = new ArrayList<>(); logger.info("【DataSource】初始化SqlSessionFactory,<START>");
arr.add("set names utf8mb4;"); final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
datasource.setConnectionInitSqls(arr); sessionFactoryBean.setDataSource(dataSource());
} catch (SQLException e) { sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
logger.error("druid configuration initialization filter", e); sessionFactoryBean.setVfs(SpringBootVFS.class);
} sessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
return datasource; sessionFactoryBean.setPlugins(new Interceptor[] { interceptor });
} return sessionFactoryBean.getObject();
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager transactionManager() { @Bean(name = "sqlSessionTemplate")
logger.info("【DataSource】初始化TransactionManager事务管理器,<START>"); @Autowired
return new DataSourceTransactionManager(dataSource()); public SqlSessionTemplate sqlSessionTemplate(ExecutorInterceptor interceptor) throws Exception {
} logger.info("【DataSource】初始化SqlSessionTemplate,<START>");
final SqlSessionTemplate sessionFactoryBean = new SqlSessionTemplate(sqlSessionFactory(interceptor));
return sessionFactoryBean;
@Bean(name = "sqlSessionFactory") }
@Autowired
public SqlSessionFactory sqlSessionFactory(ExecutorInterceptor interceptor) throws Exception { public String getUrl() {
logger.info("【DataSource】初始化SqlSessionFactory,<START>"); return url;
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); }
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); public void setUrl(String url) {
sessionFactoryBean.setVfs(SpringBootVFS.class); this.url = url;
sessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage); }
sessionFactoryBean.setPlugins(new Interceptor[]{interceptor});
return sessionFactoryBean.getObject(); public String getType() {
} return type;
}
@Bean(name = "sqlSessionTemplate")
@Autowired public void setType(String type) {
public SqlSessionTemplate sqlSessionTemplate(ExecutorInterceptor interceptor) throws Exception { this.type = type;
logger.info("【DataSource】初始化SqlSessionTemplate,<START>"); }
final SqlSessionTemplate sessionFactoryBean = new SqlSessionTemplate(sqlSessionFactory(interceptor));
return sessionFactoryBean; 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