springboot整合mysql數據庫時,要求對數據庫的用戶名和密碼進行加解密
可以使用springboot整合的加解密工具,在pom文件中引用依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
寫個main函數
public static void main(String[] arg){
StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor();
/*配置文件中配置如下的算法*/
standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES");
/*配置文件中配置的password*/
standardPBEStringEncryptor.setPassword("EWRREWRERWECCCXC");
/*要加密的文本*/
String name = standardPBEStringEncryptor.encrypt("root");
String password =standardPBEStringEncryptor.encrypt("123456");
/*將加密的文本寫到配置文件中*/
System.out.println("name="+name);
System.out.println("password="+password);
}
得到密碼
name:x1dkUSJcq+ByoSq2Z8yPrg==
password:sTrlJmqG6nBgAdi1SOSW7Q==
將得到的密碼寫在配置文件中
yml文件如下:
jasypt:
encryptor:
password: EWRREWRERWECCCXC
algorithm: PBEWithMD5AndDES
spring:
datasource:
url: xxx.xxx
username: ENC(x1dkUSJcq+ByoSq2Z8yPrg==)
password: ENC(sTrlJmqG6nBgAdi1SOSW7Q==)
proerties文件如下:
jasypt.encryptor.password=EWRREWRERWECCCXC
jasypt.encryptor.algorithm=PBEWithMD5AndDES
spring.datasource.url=xxx.xxx
spring.datasource.username=ENC(x1dkUSJcq+ByoSq2Z8yPrg==)
spring.datasource.password=ENC(sTrlJmqG6nBgAdi1SOSW7Q==)
這樣就實現了對數據庫連接的加解密了