首页 > 解决方案 > 有没有办法让 jasypt 在 Springboot 中进行自定义加密

问题描述

我需要加密 bootstrap.yml 文件中的数据库密码,为此我们决定使用 -

杰西普

我正在使用带有 spring boot 1.5.13 版本的 jasypt 1.18 版本。

最新版本的 jasypt-spring-boot:2.1.1 不支持 spring boot 1.5.13 版本,因此我将使用旧版本。

我的要求是在创建映像期间复制到映像的文件中包含密钥,并且它的路径在 bootstrap.yml 中设置

非对称加密是不可能的,因为这又出现在最新的 jar 中。

请提出如何实现这一目标的方法?

==================================================== ==============

jaspyt 提供了 3 种不同的方法来加密密码。我尝试了前两种方法,我能够成功加密/解密,但问题是密钥必须作为环境或系统属性传递。

第三种方法是使用自定义 JASYPT 加密器。我认为这个解决方案是我在寻找可以将密码保存在外部文件中并从 bootstrap.yml 传递路径的地方。

pom.xml

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

配置类

@Bean(name = "encryptorBean")
public StringEncryptor stringEncryptor() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    \\ will add code here to get the password from the file                                 
    config.setPassword("Read from a file"); 
    config.setAlgorithm("PBEWithMD5AndDES");
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setProviderName("SunJCE");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setStringOutputType("base64");
    encryptor.setConfig(config);
    return encryptor;
}

引导程序.yml

jasypt:
  encryptor:
    bean: encryptorBean  

使用此代码,我得到以下异常 -

Caused by: java.lang.IllegalStateException: Required Encryption configuration property missing: jasypt.encryptor.password
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.getRequiredProperty(DefaultLazyEncryptor.java:70) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.createDefault(DefaultLazyEncryptor.java:45) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$new$2(DefaultLazyEncryptor.java:34) ~[jasypt-spring-boot-1.18.jar:na]
    at java.util.Optional.orElseGet(Unknown Source) ~[na:1.8.0_191]
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$new$3(DefaultLazyEncryptor.java:32) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.util.Singleton.lambda$new$1(Singleton.java:20) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.util.Singleton.get(Singleton.java:31) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.decrypt(DefaultLazyEncryptor.java:82) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.resolvePropertyValue(DefaultPropertyResolver.java:35) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.resolver.DefaultLazyPropertyResolver.resolvePropertyValue(DefaultLazyPropertyResolver.java:41) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.EncryptablePropertySource.getProperty(EncryptablePropertySource.java:16) ~[jasypt-spring-boot-1.18.jar:na]
    at com.ulisesbocchio.jasyptspringboot.wrapper.EncryptableMapPropertySourceWrapper.getProperty(EncryptableMapPropertySourceWrapper.java:29) ~[jasypt-spring-boot-1.18.jar:na]
    at org.springframework.boot.bind.PropertySourcesPropertyValues.getEnumerableProperty(PropertySourcesPropertyValues.java:166) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.bind.PropertySourcesPropertyValues.processEnumerablePropertySource(PropertySourcesPropertyValues.java:149) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.bind.PropertySourcesPropertyValues.processPropertySource(PropertySourcesPropertyValues.java:128) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.bind.PropertySourcesPropertyValues.<init>(PropertySourcesPropertyValues.java:118) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.getPropertySourcesPropertyValues(PropertiesConfigurationFactory.java:331) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:285) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:250) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:331) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    ... 58 common frames omitted

为什么当我实际使用自定义加密器方法时要求输入 jasypt.encryptor.password 我在 bootstrap.yml 中定义了 bean“encryptorBean”

==================================================== ========

我还浏览了 Github,在那里他们提出了类似的问题,他们告诉使用以下依赖项,但使用这种方法我什至无法加载 jasypt。

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

https://github.com/ulisesbocchio/jasypt-spring-boot/issues/79

如果有人可以帮助我解决问题,那就太好了。

标签: spring-bootencryptionjasyptsymmetric

解决方案


将 bean 名称更改为 jasyptStringEncryptor:

> @Bean(name = "jasyptStringEncryptor") public StringEncryptor
> stringEncryptor() {

或者

设置 jasypt.encryptor.bean 属性

jasypt.encryptor.bean=encryptorBean

参考:https ://github.com/ulisesbocchio/jasypt-spring-boot#use-you-own-custom-encryptor


推荐阅读