?项目 pom 文件中添加(根据个人爱好选择相应依赖版本)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
?在依赖添加完成后,获取本地仓库中该 jar 包的地址:如 D:\Develop\Repository\localrepository\com\alibaba\druid\1.1.21,在该路径下执行如下命令生成数据库加密信息
java -cp druid-1.0.16.jar com.alibaba.druid.filter.config.ConfigTools you_password
?如密码为 root ,便会生成如下信息
D:\Develop\Repository\localrepository\com\alibaba\druid\1.1.21>java -cp druid-1.1.21.ja
r com.alibaba.druid.filter.config.ConfigTools root
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAhxXkd9KtM03lc7Q7XU2yMIWxGsVDWlvbukwHjwmBDBO4zY6m+R3vEzkJUw1C2q5WhZLBkuJgtKOc+EgLjimCQQIDAQABAkAcm4dJ1OT3I3z/NuEHjme5HOb3y2TLIv7ygh1rrlCU9N1AuIl48193MiNTA9PNx92vG9lczJuvSQMBa+QaKthBAiEAyTaEeyU03d3+IzAMZWx42RaiuCxprFTHdmhv5Oel+BkCIQCr3fvMg0VqEOs+SH/hqWMT6MIGnx32zB67cWlqSaTAaQIhAJfsdO0yM4+NzRQudD2kQD7Ra30Z85Pt00jhJBM2oMcJAiBvx3B9EuOSmG5nk7+YhRJrYHbQ16wlV5ylTJ3DAyKkAQIhAIqsg2Oao/9H2YqOD/QB3IUYDUsiBn/e0qr6pp866enj
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIcV5HfSrTNN5XO0O11NsjCFsRrFQ1pb27pMB48JgQwTuM2Opvkd7xM5CVMNQtquVoWSwZLiYLSjnPhIC44pgkECAwEAAQ==
password:cWW8QQOAlxZFjHf92XJBmWgm7mTA2fS9Jd5HKjLiewHNPlWaLKbb6PHhC5h+6RO5czSBM8zqige2gK815i+3lg==
?配置时发现一个问题,官方文档中的属性在 IDEA 会出现异常,具体配置如下:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
# 加密后的密码(原密码 123456)
spring.datasource.password=WVMjPhfXQrIsWRo0/RCqAVvYtTU9WNVToKJohb8AlUmHwnV6vwFL+FM2CNFDMJwGHW1iCmyaUlF+sgvFdogqEA==
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 公钥
publickey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIiwHpFrDijV+GzwRTzWJk8D3j3jFfhsMFJ/7k1NTvBuLgL+TdIHgaMNOIEjHpXzuvX38J3FtOK8hLrySncVGOMCAwEAAQ==
# 配置 connection-properties,启用加密,配置公钥。
spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=${publickey}
# 启动ConfigFilter
spring.datasource.druid.filter.config.enabled=true
?我在使用其配置时出现 Caused by: java.sql.SQLException: ORA-01005: null password given; logon denied 异常,若你也有同样的问题,可以使用下面的配置(将 connection-properties 属性替换成 connect-properties 即可)
?application.yml 中 Druid 部分配置如下(注意层级关系,复制粘贴在 yml 中容易丢失出现 错误),并且我这使用的是 Oracle 数据库,切记!
# 数据库连接池配置
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: root
password: # 生成的 password
druid:
connect-properties:
config.decrypt: true
config.decrypt.key: # 生成的公钥 publicKey
filter:
config:
enabled: true # 启动ConfigFilter
initial-size: 5 # 连接池初始化大小
min-idle: 10 # 最小空闲连接数
max-active: 20 # 最大连接数
原文:https://www.cnblogs.com/jrMaker/p/12578026.html