ulisesbocchio / jasypt-spring-boot

Jasypt integration for Spring boot
MIT License
2.92k stars 523 forks source link

Create custom encryptor bean with an error #365

Open MaLuxray opened 1 year ago

MaLuxray commented 1 year ago

springboot version : 2.7.0 springcloud version : 3.1.4 jasypt version : 3.0.3

When I customize an encryption and decryption springboot bean In my java code

@SpringBootConfiguration
public class JasyptConfiguration {
    private static final String secretKey = "2919de02ac6d934f130c3041f390d16c6ded91adf145efd8cd3cbb3751d71c50";

    @Bean(name = "stringEncryptor")
    public StringEncryptor stringEncryptor() {
        SimplePBEConfig config = new SimplePBEConfig();
        config.setAlgorithm("PBEWITHSHA1ANDRC2_40");
        config.setPassword(secretKey);

        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(config);
        encryptor.setPoolSize(10);
        encryptor.setStringOutputType("hexadecimal");

        return encryptor;
    }

    public static void main(String[] args) {
        JasyptConfiguration configuration = new JasyptConfiguration();
        StringEncryptor stringEncryptor = configuration.stringEncryptor();

        System.out.println(stringEncryptor.encrypt("187555566666"));
    }
}
   jasypt:
      encryptor:
          bean: stringEncryptor

When I start the service, the system reports an error

   java.lang.IllegalStateException: String Encryptor custom Bean not found with name 'stringEncryptor'
silent-night-no-trace commented 1 year ago

I also encountered this error when booting. The suspicion may be related to the loading order

silent-night-no-trace commented 1 year ago

@ulisesbocchio can you help me thank you i also meet this error

I use the following environment

spring cloud 2021.0.5 spring cloud alibaba 2021.0.5.0 spring boot 2.6.13

silent-night-no-trace commented 1 year ago

2023-08-21 13:41:20.381 ERROR [main] [,,,] org.springframework.boot.SpringApplication.reportFailure - Application run failed java.lang.IllegalStateException: String Encryptor custom Bean not found with name 'encryptorBean' at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$new$2(DefaultLazyEncryptor.java:35) at java.base/java.util.Optional.orElseGet(Optional.java:364) at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.lambda$new$3(DefaultLazyEncryptor.java:33) at com.ulisesbocchio.jasyptspringboot.util.Singleton.lambda$new$6(Singleton.java:42) at com.ulisesbocchio.jasyptspringboot.util.Singleton.get(Singleton.java:53) at com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.decrypt(DefaultLazyEncryptor.java:57) at com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.lambda$resolvePropertyValue$0(DefaultPropertyResolver.java:44) at java.base/java.util.Optional.map(Optional.java:260) at com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.resolvePropertyValue(DefaultPropertyResolver.java:40) at com.ulisesbocchio.jasyptspringboot.resolver.DefaultLazyPropertyResolver.resolvePropertyValue(DefaultLazyPropertyResolver.java:50) at com.ulisesbocchio.jasyptspringboot.EncryptablePropertySource.getProperty(EncryptablePropertySource.java:28) at com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource.getProperty(CachingDelegateEncryptablePropertySource.java:44) at com.ulisesbocchio.jasyptspringboot.wrapper.EncryptableMapPropertySourceWrapper.getProperty(EncryptableMapPropertySourceWrapper.java:29) at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:85) at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:74) at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:153) at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) at org.springframework.core.env.AbstractPropertyResolver.resolvePlaceholders(AbstractPropertyResolver.java:202) at org.springframework.core.env.AbstractEnvironment.resolvePlaceholders(AbstractEnvironment.java:625) at java.base/java.util.Optional.map(Optional.java:260) at com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.resolvePropertyValue(DefaultPropertyResolver.java:38) at com.ulisesbocchio.jasyptspringboot.resolver.DefaultLazyPropertyResolver.resolvePropertyValue(DefaultLazyPropertyResolver.java:50) at com.ulisesbocchio.jasyptspringboot.EncryptablePropertySource.getProperty(EncryptablePropertySource.java:28) at com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource.getProperty(CachingDelegateEncryptablePropertySource.java:44) at com.ulisesbocchio.jasyptspringboot.wrapper.EncryptableMapPropertySourceWrapper.getProperty(EncryptableMapPropertySourceWrapper.java:29) at org.springframework.cloud.bootstrap.encrypt.AbstractEnvironmentDecrypt.merge(AbstractEnvironmentDecrypt.java:103) at org.springframework.cloud.bootstrap.encrypt.AbstractEnvironmentDecrypt.merge(AbstractEnvironmentDecrypt.java:81) at org.springframework.cloud.bootstrap.encrypt.AbstractEnvironmentDecrypt.decrypt(AbstractEnvironmentDecrypt.java:69) at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.initialize(EnvironmentDecryptApplicationInitializer.java:95) at org.springframework.cloud.bootstrap.BootstrapApplicationListener$DelegatingEnvironmentDecryptApplicationInitializer.initialize(BootstrapApplicationListener.java:409) at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:618) at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:385)

silent-night-no-trace commented 1 year ago

The problem has been solved

see this article https://blog.csdn.net/a15835774652/article/details/132234943