Springboot 整合 jasypt
导包
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
使用
启动类上加注解
@EnableEncryptableProperties
使用
BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
basicTextEncryptor.setPassword("123");
// 加密
String zhang = basicTextEncryptor.encrypt("zhang");
// 解密
String decrypt = basicTextEncryptor.decrypt(zhang);
将加密的zhang放在配置类中使用ENC(加密的内容)
jasypt:
encryptor:
password: 123
test:
name: ENC(qiJneGfjme+e2qTFzFgesA==)
验证
package com.zhk.study;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@Value("${test.name}")
private String test;
@RequestMapping
public String get() {
return test;
}
}
将加密后的配置信息使用ENC函数,添加到配置文件中,应用启动加载配置文件时,会自动解密。一般数据库密码加密,加密指定(环境变量)