Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom encryptor bean with an error #365

Open
MaLuxray opened this issue Jun 26, 2023 · 4 comments
Open

Create custom encryptor bean with an error #365

MaLuxray opened this issue Jun 26, 2023 · 4 comments

Comments

@MaLuxray
Copy link

MaLuxray commented Jun 26, 2023

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
Copy link

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

@silent-night-no-trace
Copy link

@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
Copy link

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
Copy link

The problem has been solved

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants