springboot中druid数据源配置文件密码加密显示 - Go语言中文社区

springboot中druid数据源配置文件密码加密显示


运维和DBA都不希望把密码明文直接写在项目配置文件中,Druid提供了数据库密码加密的功能,一种数据库密码加密的手段ConfigFilter。

 

引入jar

			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>druid-spring-boot-starter</artifactId>
				<version>1.1.10</version>
			</dependency>

1、application.properties(公共参数)

spring.profiles.active=dev

server.port=80
server.servlet.context-path=/
server.tomcat.uri-encoding=UTF-8


############ logging config start ###############
logging.level.priv.hhp.cas.server=info
logging.pattern.console=%d %5p [%F:%L] : %m%n
logging.pattern.file=[%-5p][%d{yyyy-MM-dd HH:mm:ss,SSS}][%C{1}:%L] %m%n
logging.path=logs
logging.file=logs/cas-server.log
############## logging config end ###############


##################### 模板配置 start ####################
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
##################### 模板配置  end ####################


###################### 返回 Date 类型 json 格式化 #####################
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#spring-boot中对于@RestController或者@Controller+@ResponseBody注解的接口方法的返回值默认是Json格式,
# 所以当对于date类型的数据,在返回浏览器端是会被spring-boot默认的Jackson框架转换,
# 而Jackson框架默认的时区GMT(相对于中国是少了8小时)所以在application.yml中增加
spring.jackson.time-zone=GMT+8

该配置文件为springboot的默认加载文件,其中包含有一些公共配置

2、application-dev.yml(数据源配置)

# Spring Datasource Settings
spring:
  datasource:
      name: druidDataSource
      type: com.alibaba.druid.pool.DruidDataSource
      druid:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
        username: root
#        password: 123456
#        password: ${password}
#        password: config.file=classpath:conf/${spring.profiles.active}/druid.properties?password
        password: config.file=classpath:conf/${spring.profiles.active}/druid.properties
        filters: stat,wall,log4j,config
        max-active: 100
        initial-size: 1
        max-wait: 60000
        min-idle: 1
        db-type: mysql
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: select 'x'
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        pool-prepared-statements: true
        max-open-prepared-statements: 50
        max-pool-prepared-statement-per-connection-size: 20
#        connection-properties: config.decrypt=true;config.decrypt.key=${public-key}   # 启用加密,配置公钥。
        connection-properties: config.file=classpath:conf/${spring.profiles.active}/druid.properties   # 启用加密,配置公钥。
        filter:
          config:
            enabled: true   # 数据库过滤器
            
      
#      说明: 
# spring.datasource.druid.max-active 最大连接数 
# spring.datasource.druid.initial-size 初始化大小 
# spring.datasource.druid.min-idle 最小连接数 
# spring.datasource.druid.max-wait 获取连接等待超时时间 
# spring.datasource.druid.time-between-eviction-runs-millis 间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 
# spring.datasource.druid.min-evictable-idle-time-millis 一个连接在池中最小生存的时间,单位是毫秒 
# spring.datasource.druid.filters=config,stat,wall,log4j 配置监控统计拦截的filters,去掉后监控界面SQL无法进行统计,’wall’用于防火墙

该配置文件包含有数据库配置,以及其他一些配置(此地俺属于本地环境配置)

 

3、druid.properties(数据库加密密码、key以及其他参数)

# 是否加密数据库密码(该config.decrypt属性名不可更改)
config.decrypt=true
# 加密后的数据库密码(该password属性名最好与数据源配置的password一致)
password=XQ/khbjTdCEQ5qqe+lwaCVeoGicM2KlUqIuTjRKzByMs5spscVKDDhRe0S6FtSDMP4jdHxT4tJSNoGmqiFPEYw==
# 用 publicKey 解密 (该config.decrypt.key属性名不可更改)
config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAISyIUgplr69/Fc9+lkikYuXckw6pB8dBkRdcQyy9XGvQVWs4tpZb4YUIW80DaiKF9J++8N0acHJj83vduJrMC8CAwEAAQ==

# 不加密配置
#config.decrypt=false
#password=123456

druid.properties中的加密密码和key生成方式:

方式一

public class EncryptPwdUtil {
    
    public static void main(String[] args) {
	try {
	    String password = "123456";
	    String[] arr = ConfigTools.genKeyPair(512);
	    
	   // System.out.println("privateKey:" + arr[0]);
	    System.out.println("publicKey:" + arr[1]);
	    System.out.println("password:" + ConfigTools.encrypt(arr[0], password));
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}

 

方式二

在cmd控制台运行以下命令

          java -cp druid-1.1.10.jar com.alibaba.druid.filter.config.ConfigTools 你的密码

将生成的password和publicKey复制过来即可

切记:项目中引入的druid jar包版本和密码加密版本要对应,且加密的key和生成密码要对应(每次更换都更换),如:

俺都用的druid-1.1.10.jar   需要更换的时候全部重新替换。

具体看这里(druid官方): https://github.com/alibaba/druid/wiki/使用ConfigFilter

注意事项


  • 如果启动报错如下:
Caused by: java.lang.IllegalArgumentException: Failed to decrypt.

Caused by: javax.crypto.BadPaddingException: Decryption error

可能出错原因:

  1. config.decrypt.key  属性写错了;
  2. config.decrypt.key  取值错误,或许密码和该值不对应,不是同一次生成的,全部重新生成;
  • 如果报错如下:
Caused by: java.lang.IllegalArgumentException: Failed to decrypt.
	
Caused by: java.lang.IllegalArgumentException: String length must be a multiple of four.

可能出错原因:

  1. password没取到值,password属性写错了,因为默认去跟其一样的属性值;
  2. password值为空。
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_31625935/article/details/84840826
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 13:08:18
  • 阅读 ( 1947 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢