当前位置:   article > 正文

spring boot+jpa+druid 多数据源+多数据源的密码加密_多数据源加密

多数据源加密

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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/844165
推荐阅读
相关标签
  

闽ICP备14008679号