引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
编写yml配置
jasypt:
encryptor:
algorithm: PBEWithMD5AndDES #加密算法
password: cf150b74e4824146ad76e9ebe757ba76 #使用加密秘钥(自己写自己记住)
提示:如果不想密钥暴露,可以通过程序传递密钥,将密钥通过参数传给jvm -Djasypt.encryptor.password=cf150b74e4824146ad76e9ebe757ba76
加密、解密
@Autowired
StringEncryptor encryptor;
@Test
public void testSecret() {
//加密
String url = encryptor.encrypt("localhost");
System.out.println(url);
//解密
String decrypt = encryptor.decrypt("nRmCj//nOuErKepiS6dyBO/9Ff7LGCXZ");
System.out.println(decrypt);
}
使用
mysql:
host: ENC(pNVN9SKixSnfJ8yZjjIESZuP8HTwG0Hm)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mysql.host}:3306/emp?characterEncoding=UTF-8
username: ENC(M9LpI0WkgVh0rpx+BkOGew==)
password: ENC(M9LpI0WkgVh0rpx+BkOGew==)
格式:ENC(加密后的字符串)