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==)
这样就实现了对数据库连接的加解密了