引入依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
注入bean
@Configuration public class EncrypConfig {
@Bean( "jasyptStringEncryptor")
public StringEncryptor stringEncryptor(){ StandardPBEStringEncryptor encryptor=new StandardPBEStringEncryptor();
EnvironmentStringPBEConfig config= new EnvironmentStringPBEConfig(); config.setPasswordEnvName("TEST_KEY");
config.setAlgorithm("SFKUELEE_128");
encryptor.setConfig(config); return encryptor;
} }
加解密测试
@Test public void testEncyptedPassword() {
StandardPBEStringEncryptor encryptor=new StandardPBEStringEncryptor(); EnvironmentStringPBEConfig config= new EnvironmentStringPBEConfig(); config.setPasswordEnvName("TEST_KEY");
config.setAlgorithm("SFKUELEE_128");
encryptor.setConfig(config);
String encrypPassword = encryptor.encrypt(originalPassword);
log.info(encryptedPassword);
log.info(encryptor.decrypt("sdfgjlsd"));
}
修改配置文件
password: ENC(加密文)