前言
公司两个项目使用druid加密配置,连接相同数据库。其中一个项目加密配置无效,后面定位发现是兼容性问题使用druid-spring-boot-starter代替druid即可。
pom配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- springboot直接使用原生druid会有兼容性问题,无法读到加密配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.4</version>
</dependency>
-->
生成加密配置
执行如下命令:
java -cp druid-1.1.10.jar com.alibaba.druid.filter.config.ConfigTools you_password
或
public static void main(String[] args) throws Exception {
ConfigTools.main(new String[]{"123456"});
}
输出
privateKey:MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAjYD6iWcSu+SU6djwf4r7PfPahOBPXYo+zdxTzkx++7pWn961/nuXWMEPlFxwAIcXHnv1itp5DZjHCPFz1VdyxQIDAQABAkAE18w7VabDfYBHPKCGjPgqV6U79P1iYy/hFdBceCvEa7/sQ0jOohcVOxKePczG4iJ8hKdk41wzWMDG6BafZ/2FAiEA6XBcVqRRd0T0yzudFIcaNglGgJ3pIQ9c1N0Ej3yNdsMCIQCbLgB7NcSUV5M3wZZ+5EOFXBG+QEmsJHlV2THEj7sn1wIgMZB82pya+4L1ou0uZfxlkVGY2ZgttqpHYJ7o5/pfIvcCIBBTe6t60a7Fs/Bmxp2dCFmMgTVh8B5TTAyzKCBE+uzLAiBKMfR0ZhlnygjqBlgmjbB6cCxwdiutxIpgo1DdpWbhSQ==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI2A+olnErvklOnY8H+K+z3z2oTgT12KPs3cU85Mfvu6Vp/etf57l1jBD5RccACHFx579YraeQ2Yxwjxc9VXcsUCAwEAAQ==
password:ECnsXbbkRu/QxtAOZFj/xW9Zgbt7m0p9XKGVamBopsaPXnbe09HkXADY6MgzB9UX5SEySQwWmXg4/xKdz/zxAQ==
连接配置application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
# 生成的加密后的密码(原密码 123456)
spring.datasource.password=WVMjPhfXQrIsWRo0/RCqAVvYtTU9WNVToKJohb8AlUmHwnV6vwFL+FM2CNFDMJwGHW1iCmyaUlF+sgvFdogqEA==
# 生成的公钥
public-key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIiwHpFrDijV+GzwRTzWJk8D3j3jFfhsMFJ/7k1NTvBuLgL+TdIHgaMNOIEjHpXzuvX38J3FtOK8hLrySncVGOMCAwEAAQ==
# 配置 connection-properties,启用加密,配置公钥。
spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=${public-key}
# 启用ConfigFilter
spring.datasource.druid.filter.config.enabled=true