赞
踩
spring boot和jpa整合,使用druid作为连接池,在单数据源时加密比较容易,但是多数据源时加密遇到问题,搜索了几个博文
https://blog.csdn.net/ooobama/article/details/80752223
文中给到了解决方案,就是手动解密。
该博文已经明确了解决方案,这里不在重复,但是博文没有说清楚多数据源的配置和配置文件的详情,我这里把我跑成功的贴一下。
package com.***; import com.alibaba.druid.pool.DruidDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; import javax.sql.DataSource; /** * Created by chenglong on 2018-12-26 13:58 */ @Configuration public class DataSourceConfig { private static final Logger logger = LoggerFactory.getLogger(DataSourceConfig.class); @Value("${spring.datasource.primary.jdbc-url}") private String dbUrl; @Value("${spring.datasource.primary.username}") private String username; @Value("${spring.datasource.primary.password}") private String password; @Value("${spring.datasource.primary.driver-class-name}") private String driverClassName; @Value("${spring.datasource.secondary.jdbc-url}") private String dbUrl2; @Value("${spring.datasource.secondary.username}") private String username2; @Value("${spring.datasource.secondary.password}") private String password2; @Value("${spring.datasource.secondary.driver-class-name}") private String driverClassName2; @Value("${spring.datasource.primary.type}") private String dbType; @Value("${spring.druid.initial-size}") private int initialSize; @Value("${spring.druid.min-idle}") private int minIdle; @Value("${spring.druid.max-active}") private int maxActive; @Value("${spring.druid.max-wait}") private int maxWait; @Value("${spring.druid.time-between-eviction-runs-millis}") private int timeBetweenEvictionRunsMillis; @Value("${spring.druid.min-evictable-idle-time-millis}") private int minEvictableIdleTimeMillis; @Value("${spring.druid.validation-query}") private String validationQuery; @Value("${spring.druid.test-while-idle}") private boolean testWhileIdle; @Value("${spring.druid.test-on-borrow}") private boolean testOnBorrow; @Value("${spring.druid.test-on-return}") private boolean testOnReturn; @Value("${spring.druid.pool-prepared-statements}") private boolean poolPreparedStatements; @Value("${spring.druid.max-pool-prepared-statement-per-connection-size}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.primary.connection-properties}") private String connectionProperties; @Value("${spring.datasource.secondary.connection-properties}") private String connectionProperties2; @Value("${spring.druid.password-callback}") private String passwordCallbackClassName; @Bean(name = "primaryDataSource") @Primary @ConfigurationProperties(prefix="spring.datasource.primary") public DataSource primaryDataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(dbUrl); 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.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); datasource.setConnectionProperties(connectionProperties); datasource.setDbType(dbType); try { datasource.setPasswordCallbackClassName(passwordCallbackClassName); } catch (Exception e) { logger.error("druid configuration initialization passwordCallbackClassName", e); } return datasource; } @Bean(name = "secondaryDataSource") @ConfigurationProperties(prefix="spring.datasource.secondary") public DataSource secondaryDataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(dbUrl2); datasource.setUsername(username2); datasource.setPassword(password2); datasource.setDriverClassName(driverClassName2); 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.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); datasource.setDbType(dbType); datasource.setConnectionProperties(connectionProperties); try { datasource.setPasswordCallbackClassName(passwordCallbackClassName); } catch (Exception e) { logger.error("druid configuration initialization passwordCallbackClassName", e); } return datasource; } }
application.yml配置文件
spring: datasource: #本地数据库 primary: driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource jdbc-url: jdbc:mysql://***/***?characterEncoding=utf8&useSSL=false username: *** password: G0jxH***auCnXwjsEHZX4ueA41C4KA== # 配置 connection-properties,启用加密,配置公钥。 connection-properties: config.decrypt=true;publickey=${spring.datasource.primary.publickey};password=${spring.datasource.primary.password} # 公钥 publickey: MFwwDQYJ***RxcJ1Ch77tBggspntMwQgTyVRgMU/Z2NAMBN0wM1x1Ux+T/Pqxsu9ra++GgbM910CAwEAAQ== #外部数据库 secondary: driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://***/***?characterEncoding=utf8&useSSL=false username: *** password: VJa9HBxSKFKM6z3cUg+GmD***7xCHrBx+IHKtb3DQpZRMHism6NBRLMP99nfu/A== type: com.alibaba.druid.pool.DruidDataSource # 配置 connection-properties,启用加密,配置公钥。 connection-properties: config.decrypt=true;publickey=${spring.datasource.secondary.publickey};password=${spring.datasource.secondary.password} # 公钥 publickey: MFwwDQYJKo******WbG2nyHEqv5Txpegdu6B9e6VuvTU6Qy8fE0C***+95oHED028CAwEAAQ== druid: initial-size: 2 max-active: 15 min-idle: 2 max-wait: 1234 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 5 validation-query: select 1 from dual validation-query-timeout: 1 test-on-borrow: true test-on-return: true test-while-idle: true time-between-eviction-runs-millis: 10000 min-evictable-idle-time-millis: 30001 async-close-connection-enable: true filter: stat: db-type: h2 log-slow-sql: true slow-sql-millis: 2000 # 启动ConfigFilter config: enabled: true password-callback: com.***.data.common.utils.DbPasswordCallback jpa: database: MYSQL show-sql: true hibernate: ddl-auto: update naming: implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。