java - 使用 attributeconverter 时,Spring Boot 应用程序未完成启动

标签 java spring spring-boot encryption dependency-injection

已编辑:在添加最新的 AttributeConverter 代码之前,该应用程序启动良好并运行良好。我怀疑这是由于 Autowiring 顺序造成的。因为如果我从属性转换器中删除 Autowiring 的 bean,应用程序就会正常启动。

编辑:cipherCreator、SecretKey 的代码

我尝试在将值保存到数据库时使用加密。我实现了一个属性转换器类和另一个类,用于初始化密码以及注入(inject) key 的配置值。

当我用 AttributeConverter 装饰实体类进行加密时,框架未完成启动。

它在 ===> 触发 Spring Data 存储库的延迟初始化后停止。它不会抛出任何异常来停止。只是坐在那里。如果我从密码创建器中删除自动连线注释,它就会启动。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)

2020-08-22 17:10:50.808  INFO 23468 --- [           main] c.c.b.data.xyzApplication   : The following profiles are active: secret,default
2020-08-22 17:10:53.811  INFO 23468 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-08-22 17:10:54.165  INFO 23468 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 340ms. Found 7 JPA repository interfaces.
2020-08-22 17:10:54.812  INFO 23468 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=65cd15a6-6963-3882-983e-554bc1ab6bc0
2020-08-22 17:10:55.187  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.189  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.190  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.192  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.194  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.346  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a2393a66] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:56.480  INFO 23468 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-22 17:10:56.502  INFO 23468 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-22 17:10:56.502  INFO 23468 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-08-22 17:10:56.709  INFO 23468 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-22 17:10:56.709  INFO 23468 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5867 ms
2020-08-22 17:10:57.755  INFO 23468 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-22 17:10:57.840  INFO 23468 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-08-22 17:11:00.487  INFO 23468 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-08-22 17:11:00.747  INFO 23468 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-08-22 17:11:00.879  INFO 23468 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.15.Final
2020-08-22 17:11:01.165  WARN 23468 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-08-22 17:11:01.307  INFO 23468 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-08-22 17:11:01.467  INFO 23468 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories�
2020-08-22 17:11:01.951  INFO 23468 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-08-22 17:11:02.470  WARN 23468 --- [         task-1] org.hibernate.cfg.AnnotationBinder       : 

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Base64;

@Component
@Converter
public class CustomerEncryptor implements AttributeConverter<String, String>{

    @Autowired
    CipherCreator cipherCreator;

    @Override
    public String convertToDatabaseColumn(String attribute) {
        if(StringUtils.isEmpty(attribute))
            return attribute;
        try {
            return do something...
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String convertToEntityAttribute(String dbData) {
        if(StringUtils.isEmpty(dbData))
            return dbData;
        try {
            do something...
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

我注意到如果我不使用

    [@]Autowired
    CipherCreator cipherCreator;

应用程序启动。

任何想法都值得赞赏

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

@Setter
@Getter
@Component
public class CipherCreator {
    private static String ALGORITHM = "AES";
    private Key key;
    private Cipher cipherEnc;
    private Cipher cipherDec;

    @Autowired
    public CipherCreator(SecretKeyProperty keyProperty) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        key = new SecretKeySpec(keyProperty.getSECRETKEY().getBytes(), ALGORITHM);

        cipherEnc = Cipher.getInstance(ALGORITHM);
        cipherEnc.init(Cipher.ENCRYPT_MODE, key);
        cipherDec = Cipher.getInstance(ALGORITHM);
        cipherDec.init(Cipher.DECRYPT_MODE, key);
    }
}
@Component
public class SecretKeyProperty {

    public String SECRETKEY;


    @Autowired
    public SecretKeyProperty(@Value("${encryption.key}") final String key)  {
        this.SECRETKEY = key;
    }

    public String getSECRETKEY() {
        return SECRETKEY;
    }

    public void setSECRETKEY(String sECRETKEY) {
        SECRETKEY = sECRETKEY;
    }

}

最佳答案

这是由于 boostrap mode in JPA 更改的结果。引用更改:

Change the general default to deferred – it parallelizes the bootstrap of the EntityManagerFactory and delays repository initialization to the end of the ApplicationContext initialization

要解决您的问题,请使用默认引导模式 (spring.data.jpa.repositories.bootstrap-mode=default)。

(感谢 VMware 的 Stéphane Nicoll 在这方面的帮助,请参阅 this discussion at spring github )

关于java - 使用 attributeconverter 时,Spring Boot 应用程序未完成启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63542462/

相关文章:

java - 使用 GitHub 生成的 HMAC 十六进制摘要与 Java 提供的摘要不匹配

java - 如何在 Spring Boot 中使用带有构造函数注入(inject)的 YAML 属性?

java - Spring Boot - 执行器 - 映射 : only own handler

java - Bean创建异常: Error creating bean with name 'userRepository' : Post-processing of merged bean definition failed

java - Hadoop FairScheduler 的高级内部数据结构和算法?

java - GWT + Hibernate 多对一 xml O/R 映射 : SerializationException

java - Spring Boot 测试 - @WithMockUser 发布的内容为空

maven - 启动 spring boot "An attempt was made to call a method that does not exist"时出错

java - HH : MM in Range Bar

java - 上下文路径与 webapp-runner 中的 Spring UrlTag 冲突?