使用Jasypt和自定义StringEncryptor

2022-01-28 23:03:20 标签 javaspring-bootjasypt

我已经成功地使用了默认的jasypt配置来加密Springboot中数据库连接的密码。然而,我非常喜欢把“秘密”存储在罐子里,而不是在环境中。我从文档中使用了一个自定义的StringEncryptor类。

@Bean(name = "jasyptStringEncryptor")
public StringEncryptor getPasswordEncryptor() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
     
    config.setPassword("verybigsecret");
     
    config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setProviderName("SunJCE");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setStringOutputType("base64");
    encryptor.setConfig(config);
    return encryptor;
}

然后使用mvn目标在资源文件中插入加密的密钥(也可以使用application。properties)

mvn jasypt:encrypt -Djasypt.encryptor.password=verybigpassword -Djasypt.plugin.path="file:src/main/resources/default.properties" -Djasypt.encryptor.StringEncryptor=jasyptStringEncryptor

然而,我得到这个错误,表明目标无法找到自定义bean

[INFO] No active profile set, falling back to default profiles: default
[INFO] Post-processing PropertySource instances
[INFO] Converting PropertySource configurationProperties [org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource] to AOP Proxy
[INFO] Converting PropertySource systemProperties [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
[INFO] Converting PropertySource systemEnvironment [org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor$OriginAwareSystemEnvironmentPropertySource] to EncryptableSystemEnvironmentPropertySourceWrapper
[INFO] Converting PropertySource random [org.springframework.boot.env.RandomValuePropertySource] to EncryptablePropertySourceWrapper
[INFO] Property Filter custom Bean not found with name 'encryptablePropertyFilter'. Initializing Default Property Filter
[INFO] Started MavenCli in 0.456 seconds (JVM running for 1.403)
[INFO] Active Profiles: Default
[INFO] Encrypting file src/main/resources/default.properties
[INFO] String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing Default String Encryptor
[INFO] Encryptor config not found for property jasypt.encryptor.algorithm, using default value: PBEWITHHMACSHA512ANDAES_256

我已经在这个问题上工作了一段时间,但找不到问题-我猜它是明显的,但我无法看到它。任何援助赞赏!

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码