springboot中jasypt自定义加密解密使用
加密命令:
java -cp ./jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI algorithm=PBEWithMD5AndDES password=suyu.net input=test
解密命令:
java -cp ./jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI algorithm=PBEWithMD5AndDES password=suyu.net input=JKTA6O9YoXtcgZFaGb1JSw==
maven依赖:
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.4</version> </dependency>
配置使用:
ENC(JKTA6O9YoXtcgZFaGb1JSw==)
配置类:MyEncryptorConfig
package net.suyu.demo.configure; import com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties; import org.jasypt.encryption.StringEncryptor; import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(JasyptEncryptorConfigurationProperties.class) public class MyEncryptorConfig { private final String password="suyu.net"; /** * 默认找jasyptStringEncryptor这个bean,这里直接取这个名字进行注册,省去额外配置 * @param properties * @return */ @Bean StringEncryptor jasyptStringEncryptor(JasyptEncryptorConfigurationProperties properties){ return new StringEncryptor() { // 加密 @Override public String encrypt(String message) { // todo 加密实现 return message; } // 解密 @Override public String decrypt(String encryptedMessage) { // todo 解密实现 PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); encryptor.setPassword(password); encryptor.setAlgorithm("PBEWithMD5AndDES"); encryptor.setPoolSize(1); return encryptor.decrypt(encryptedMessage); } }; } }