jasypt加密

Jasypt Spring Boot 为 Spring Boot 应用程序中的属性源提供加密支持。
有 3 种方法可以集成jasypt-spring-boot到您的项目中:

  • jasypt-spring-boot-starter如果使用@SpringBootApplication@EnableAutoConfiguration将在整个 Spring 环境中启用可加密属性,只需将启动器 jar 添加到您的类路径
  • 添加jasypt-spring-boot到您的类路径并添加@EnableEncryptableProperties到您的主配置类以在整个 Spring 环境中启用可加密属性
  • 添加jasypt-spring-boot到您的类路径并声明单独的可加密属性源@EncrytablePropertySource

 

1 pom.xml文件添加依赖 

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

2 参数

Key Required Default Value
jasypt.encryptor.password True -
jasypt.encryptor.algorithm False PBEWITHHMACSHA512ANDAES_256
jasypt.encryptor.key-obtention-iterations False 1000
jasypt.encryptor.pool-size False 1
jasypt.encryptor.provider-name False SunJCE
jasypt.encryptor.provider-class-name False null
jasypt.encryptor.salt-generator-classname False org.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.iv-generator-classname False org.jasypt.iv.RandomIvGenerator
jasypt.encryptor.string-output-type False base64
jasypt.encryptor.proxy-property-sources False false
jasypt.encryptor.skip-property-sources False empty list

2 cmd方式加密密码

input:为要加密的密码

password:密钥

algorithm:采用的加密算法

java -cp C:/Users/Administrator/.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=qwer*1234* algorithm=PBEWithMD5AndDES

3 工具类加解密:

@Slf4j
public class JasyptUtil {
 
    /**
     * 加密
     * @param text 需要加密的字符串
     * @return String
     */
    public static String encrypt(String text) {
        return encrypt(text,"scrm");
    }
 
    /**
     *  解密
     * @param text 需要加密的字符串
     * @param password 加密密码
     * @return String
     */
    public static String encrypt(String text,String password) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        //自己在用的时候更改此密码
        config.setPassword(password);
        //应用配置
        encryptor.setConfig(config);
        return encryptor.encrypt(text);
    }
 
    /**
     *  解密
     * @param text 需要加密的字符串
     * @return String
     */
    public static String decrypt(String text) {
        return decrypt(text,"scrm");
    }
 
    /**
     *  解密
     * @param text 需要解密的字符串
     * @param password 解密密码
     * @return String
     */
    public static String decrypt(String text,String password) {
 
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        //自己在用的时候更改此密码
        config.setPassword(password);
        //应用配置
        encryptor.setConfig(config);
 
        //解密
        return encryptor.decrypt(text);
    }
}

4 配置文件中 

jasypt.encryptor.algorithm = scrm或者zstest
jasypt.encryptor.algorithm = PBEWithMD5AndDES
jasypt.encryptor.iv-generator-classname = org.jasypt.iv.NoIvGenerator
 
spring.datasource.password = ENC(HTkX8gEYcsi60YFmsoGCrQ==)

二:原理

2.1、执行过程

2.2、主要组件

StringEncryptor 加密解密处理器

EncryptablePropertySourceConverter  用于调用配置文件转件器

EncryptablePropertyResolver 配置处理器

EncryptablePropertyDetector 是否加密的检查器

EncryptablePropertyFilter 过滤器,用于过滤哪些class无需处理

posted @ 2022-06-16 16:30  姚春辉  阅读(3804)  评论(0编辑  收藏  举报