Spring Boot 配置 - 配置信息加密

▶ Spring Boot 依赖与配置

Maven 依赖

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            com.github.ulisesbocchio
            jasypt-spring-boot-starter
            2.1.1
        

▶ 使用说明

假设有配置项 com.anoyi.custom.name=anoyi 不能明文显示,则可以使用 jasyptPBEWithMD5AndDES 算法加密算法进行如下配置:

com.anoyi.custom.name=ENC(TqrnYZn55aFVwnSo2TrbFA==)
jasypt.encryptor.password=anoyi
  • jasypt.encryptor.password 为自定义值,用此密码加密的明文,需要用此密码解密密文
  • ENC(...)jasypt 提供的加密标识,Spring Boot 服务启动时,加载各种 properties 时会依据此标识判断是否解密赋值,可自定义
  • TqrnYZn55aFVwnSo2TrbFA== 为明文字符串 anoyi 通过密码 anoyi 加密后得到的值,此值不唯一,即同一明文通过同一密码加密会得到不同的值

▶ 配置说明

基于 Password 的加密配置

参数 必填 默认值
jasypt.encryptor.password True -
jasypt.encryptor.algorithm False PBEWithMD5AndDES
jasypt.encryptor.keyObtentionIterations False 1000
jasypt.encryptor.poolSize False 1
jasypt.encryptor.providerName False SunJCE
jasypt.encryptor.providerClassName False null
jasypt.encryptor.saltGeneratorClassname False org.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.ivGeneratorClassname False org.jasypt.salt.NoOpIVGenerator
jasypt.encryptor.stringOutputType False base64
jasypt.encryptor.proxyPropertySources False false

最新版的 jasypt 还支持非对称加密、自定义加密器等等功能,更多信息:

MORE :https://github.com/ulisesbocchio/jasypt-spring-boot

▶ 配置参数加解密

添加依赖

        
            org.springframework.boot
            spring-boot-starter-test
        

示例加解密字符串 anoyi

@RunWith(SpringRunner.class)
@SpringBootTest
public class EncryptTest {

    @Autowired
    private StringEncryptor jasyptStringEncryptor;

    @Test
    public void encrypt() {
        String encryptStr = jasyptStringEncryptor.encrypt("anoyi");
        System.out.println(encryptStr);
    }

    @Test
    public void decrypt() {
        String encryptStr = jasyptStringEncryptor.decrypt("TqrnYZn55aFVwnSo2TrbFA==");
        System.out.println(encryptStr);
    }

}

▶ Github Demo URL

  • https://github.com/ChinaSilence/spring-boot-demos/tree/master/05%20-%20config%20encrypt

你可能感兴趣的:(Spring Boot 配置 - 配置信息加密)