spring boot 中阿里druid 数据源配置及密码加密
2、進行數據庫明文加密
java -cp druid-1.0.16.jar com.alibaba.druid.filter.config.ConfigTools you_password
如下加密:
privateKey:MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAh12hnaZuMe76Yb4pi7ogSAEMOcavmz7Blo8DYxeipxeZQhnrXngxc0gAQ6ORlofLWtDm6S7bI7wfDT2EFy/2DwIDAQABAkABMRjYK3vy4pi/vY3eFhBssd2qsI4hPsczjSTJfY7IC9Dc1f7g0axTM6Cx68tRUwv0rSnUiJ5EcDEhuD0JusSZAiEAwX1HpCTq8QgBV1WriHQC7Cd/9Qqp1V4yJeA/jdvXhbsCIQCzGS6wdTQCXDZKLvjRLeSUyTmmIqV/wckqdnpMUZ2BvQIgBIamr1tBt6OlTGKvoYB9NQLzhkrakCgk6ifltK7IytMCIBIbf67zipiafhqt+RYdD7lDRwLXCeiKzS3v4JmKvuP5AiEAr+zqD6sdXv7rWjqu50n+LXbWtNP/M4JzzO1mJOHEhoE= publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIddoZ2mbjHu+mG+KYu6IEgBDDnGr5s+wZaPA2MXoqcXmUIZ6154MXNIAEOjkZaHy1rQ5uku2yO8Hw09hBcv9g8CAwEAAQ== password:Y464AerH8tabxQg5DlkUej6gQ64KY73ahgiPyaB0vguLBLjUEEkVu6VBueiXxcnMfVjh1Nbd+lJNUTnS1a3/xg==
配置密碼:
privateKey:MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAh12hnaZuMe76Yb4pi7ogSAEMOcavmz7Blo8DYxeipxeZQhnrXngxc0gAQ6ORlofLWtDm6S7bI7wfDT2EFy/2DwIDAQABAkABMRjYK3vy4pi/vY3eFhBssd2qsI4hPsczjSTJfY7IC9Dc1f7g0axTM6Cx68tRUwv0rSnUiJ5EcDEhuD0JusSZAiEAwX1HpCTq8QgBV1WriHQC7Cd/9Qqp1V4yJeA/jdvXhbsCIQCzGS6wdTQCXDZKLvjRLeSUyTmmIqV/wckqdnpMUZ2BvQIgBIamr1tBt6OlTGKvoYB9NQLzhkrakCgk6ifltK7IytMCIBIbf67zipiafhqt+RYdD7lDRwLXCeiKzS3v4JmKvuP5AiEAr+zqD6sdXv7rWjqu50n+LXbWtNP/M4JzzO1mJOHEhoE= publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIddoZ2mbjHu+mG+KYu6IEgBDDnGr5s+wZaPA2MXoqcXmUIZ6154MXNIAEOjkZaHy1rQ5uku2yO8Hw09hBcv9g8CAwEAAQ== password:Y464AerH8tabxQg5DlkUej6gQ64KY73ahgiPyaB0vguLBLjUEEkVu6VBueiXxcnMfVjh1Nbd+lJNUTnS1a3/xg==
訪問本地Druid??http://localhost:8080/druid/
方式二:
1、pom.xml引入druid包
?2、com.alibaba.druid.filter.config.ConfigTools生產秘鑰
public final class DruidEncryptorUtils {
? ? private static String privateKey;
? ? private static String publicKey;
? ? static {
? ? ? ? try {
? ? ? ? ? ? String[] keyPair = ConfigTools.genKeyPair(512);
? ? ? ? ? ? privateKey = keyPair[0];
? ? ? ? ? ? System.out.println(String.format("privateKey-->%s",privateKey));
? ? ? ? ? ? publicKey = keyPair[1];
? ? ? ? ? ? System.out.println(String.format("publicKey-->%s",publicKey));
? ? ? ? } catch (NoSuchAlgorithmException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (NoSuchProviderException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? @SneakyThrows
? ? public static String encode(String plaintext){
? ? ? ? System.out.println("明文字符串:" + plaintext);
? ? ? ? String ciphertext = ConfigTools.encrypt(privateKey,plaintext);
? ? ? ? System.out.println("加密后字符串:" + ciphertext);
? ? ? ? return ciphertext;
? ? }
? ??
? ? @SneakyThrows
? ? public static String decode(String ciphertext){
? ? ? ? System.out.println("加密字符串:" + ciphertext);
? ? ? ? String plaintext = ConfigTools.decrypt(publicKey,ciphertext);
? ? ? ? System.out.println("解密后的字符串:" + plaintext);
? ? ? ? return plaintext;
? ? }
3、修改數據鏈接datasourec配置文件
? ? 3.1、filter開啟config 、配置connectionProperties
? ? ? ?filter:
? ? ? ? ?config: enabled: true
? ? connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.publickey}(公鑰秘鑰)
??
?3.2、數據庫配置模版
pring:
? ? datasource:
? ? ? ? type: com.alibaba.druid.pool.DruidDataSource
? ? ? ? driverClassName: com.mysql.cj.jdbc.Driver
? ? ? ? url: ${DATASOURCE_URL:jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai}
? ? ? ? username: ${DATASOURCE_USERNAME:root}
? ? ? ? password: ${DATASOURCE_PWD:MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQDgip8B3yVQxTyt==}
? ? ? ? publickey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4IqfAd8lUMU8rfNsvCCG+ejqAjRWdQSCrWdBlB7D1phrgnMv1QdG4MsCFAR7yf4gCKjSN4Ql9BRfY5zDJgl7
? ? ? ? druid:
? ? ? ? ? ? # 初始連接數
? ? ? ? ? ? initialSize: 5
? ? ? ? ? ? # 最小連接池數量
? ? ? ? ? ? minIdle: 10
? ? ? ? ? ? # 最大連接池數量
? ? ? ? ? ? maxActive: 20
? ? ? ? ? ? # 配置獲取連接等待超時的時間
? ? ? ? ? ? maxWait: 60000
? ? ? ? ? ? # 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
? ? ? ? ? ? timeBetweenEvictionRunsMillis: 60000
? ? ? ? ? ? # 配置一個連接在池中最小生存的時間,單位是毫秒
? ? ? ? ? ? minEvictableIdleTimeMillis: 300000
? ? ? ? ? ? # 配置一個連接在池中最大生存的時間,單位是毫秒
? ? ? ? ? ? maxEvictableIdleTimeMillis: 900000
? ? ? ? ? ? filter:
? ? ? ? ? ? ? ? config:
? ? ? ? ? ? ? ? ? ? enabled: true
? ? ? ? ? ? connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.publickey}
方式三:jasypt加密
?1、pom.xml引入jasypt包
<dependency>
? ?<groupId>com.github.ulisesbocchio</groupId>
? ?<artifactId>jasypt-spring-boot-starter</artifactId>
</dependency>
2、jasypt加密工具類
public final class JasyptEncryptorUtils {
? ? private static final String salt = "platfrom";
? ? private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
? ? static {
? ? ? ? basicTextEncryptor.setPassword(salt);
? ? }
? ? private JasyptEncryptorUtils(){}
?
? ? public static String encode(String plaintext){
? ? ? ? System.out.println("明文字符串:" + plaintext);
? ? ? ? String ciphertext = basicTextEncryptor.encrypt(plaintext);
? ? ? ? System.out.println("加密后字符串:" + ciphertext);
? ? ? ? return ciphertext;
? ? }
? ?
? ? public static String decode(String ciphertext){
? ? ? ? System.out.println("加密字符串:" + ciphertext);
? ? ? ? ciphertext = "ENC(" + ciphertext + ")";
? ? ? ? if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)){
? ? ? ? ? ? String plaintext = PropertyValueEncryptionUtils.decrypt(ciphertext,basicTextEncryptor);
? ? ? ? ? ? System.out.println("解密后的字符串:" + plaintext);
? ? ? ? ? ? return plaintext;
? ? ? ? }
? ? ? ? System.out.println("解密失敗");
? ? ? ? return "";
? ? }
}
3、數據庫配置
jasypt:
? encryptor:
? ? ? password: root
? ? ? algorithm: PBEWithMD5AndDES
? ? ? iv-generator-classname: org.jasypt.iv.NoIvGenerator
4、數據庫配置
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: ENC(TiGjk8ijFXoyI+LTqnckIfe6ogrlWwSd4xPp5MRyOvv7W+JeFWax9qT50hmNotK/qb6Z7+ip3+c6TXXt/YeyckM/7hDHXv+L9mXxgC+NEWE3OUnSeWYhJ2yMyy4eN4d1D4U+dAr7lLFCCGf8nRLEujV/Ii8ayPCi4RMgLlp8+yw=)username: ENC(GzUxN13/FoeU9uwRI4iylA==)password: ENC(p32urgxaD7/ztSfJhF4JAQ==)type: com.alibaba.druid.pool.DruidDataSourcedruid:# 連接池的配置信息# 初始化時建立物理連接的個數initial-size: 3# 連接池最小連接數min-idle: 3# 連接池最大連接數max-active: 20# 獲取連接時最大等待時間,單位毫秒max-wait: 60000# 申請連接的時候檢測,如果空閑時間大于timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。test-while-idle: true# 既作為檢測的間隔時間又作為testWhileIdel執行的依據time-between-connect-error-millis: 60000# 銷毀線程時檢測當前連接的最后活動時間和當前時間差大于該值時,關閉當前連接min-evictable-idle-time-millis: 30000jasypt:encryptor:password: platfromalgorithm: PBEWithMD5AndDESiv-generator-classname: org.jasypt.iv.NoIvGenerator5、pom.xml jar包設置? ? ?
? ? ? ? ? 5.1、idea/clipse 默認從maven下載 無法下載阿里maven倉庫jasypt-spring-boot-starter包
? ?
5.2、方法一:打開 maven 的配置文件( windows 機器一般在 maven 安裝目錄的?conf/settings.xml?),在<mirrors></mirrors>標簽中添加 mirror 子節點:
<mirror>
? ? <id>aliyunmaven</id>
? ? <mirrorOf>*</mirrorOf>
? ? <name>阿里云公共倉庫</name>
? ? <url>https://maven.aliyun.com/repository/public</url>
</mirror>
??如果想使用其它代理倉庫,可在<repositories></repositories>節點中加入對應的倉庫使用地址。以使用 spring 代理倉為例:
<repository>
? ? ? <id>spring</id>
? ? ?<url>https://maven.aliyun.com/repository/spring</url>
? <releases>
? ? ? ?<enabled>true</enabled>
? </releases>
? <snapshots>
? ? ? ? ? <enabled>true</enabled>
? </snapshots>
</repository>
在你的?pom.xml?文件<denpendencies></denpendencies>節點中加入你要引用的文件信息:
<dependency>
? ?<groupId>[GROUP_ID]</groupId>
? ?<artifactId>[ARTIFACT_ID]</artifactId>
? ?<version>[VERSION]</version>
</dependency>
執行拉取命令:
? ? ?mvn install
5.2、方法二:直接阿里云開發者社區-云計算社區-阿里云下載jar到本地maven倉庫Jasypt相關包都需下載
? 手動加入到idea項目里面?
?
總結
以上是生活随笔為你收集整理的spring boot 中阿里druid 数据源配置及密码加密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 淘集集官宣破产,创业不易,且行且珍惜
- 下一篇: java生成二维码,并在前端展示。