首页 > 解决方案 > Spring Boot Security、JPA、WebMVC和Cache之间的Spring Boot循环依赖冲突

问题描述

使用 JPA 数据库连接构建 Web Spring Boot 应用程序。到目前为止工作正常。现在,当使用公司内部库实现 Spring Boot Security 时,该库丰富了原始 Spring Security 依赖项,我开始面临循环依赖问题。

安慰:

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   servletEndpointRegistrar defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]
      ↓
   cachesEndpoint defined in class path resource [org/springframework/boot/actuate/autoconfigure/cache/CachesEndpointAutoConfiguration.class]
┌─────┐
|  cacheManager defined in class path resource [org/springframework/boot/autoconfigure/cache/CaffeineCacheConfiguration.class]
↑     ↓
|  cacheManagerCustomizers defined in class path resource [org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.class]
↑     ↓
|  jwtAppSecurityConfigurer (field private org.springframework.security.authentication.AuthenticationManager internal.company.library.package.AbstractPreAuthSecurityConfigurer.authenticationManager)
↑     ↓
|  securityConfig (This is my SecurityConfig Class posted below)
↑     ↓
|  org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
↑     ↓
|  openEntityManagerInViewInterceptorConfigurer defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration$JpaWebConfiguration.class]
↑     ↓
|  openEntityManagerInViewInterceptor defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration$JpaWebConfiguration.class]
└─────┘

安全配置类:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // This bean is created by internal spring authentication library
    @Autowired
    private JwtAppSecurityConfigurer jwtAppSecurityConfigurer;

    // Authorize everyone and everything for the moment
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/*").permitAll();
        // Apply jwtAppSecurityConfigurer
        jwtAppSecurityConfigurer.configure(httpSecurity);
    }

    // Exposing AuthenticationManager needed by internal library
    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

到目前为止我尝试了什么:

任何人都知道我可以做些什么来打破控制台中提到的 bean 之间的循环依赖关系?

编辑1:

application.properties(仅显示 JAP/数据库相关配置):

# ===============================
# = DATA SOURCE
# ===============================
# HikariCP settings
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=5

# database connection config
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url= XXXXXXXXXX
# User and PW currently hard coded
spring.datasource.username= XXXXXXXXXX
spring.datasource.password= XXXXXXXXX

# Keep db connection alive
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1

# logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.level.org.hibernate.SQL=debug
org.springframework.beans.factory=ALL

# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.hibernate.ddl-auto=none

#Naming Strategy
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
# spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

标签: javaspringspring-bootjwtcircular-dependency

解决方案


推荐阅读