java - 无法在自定义 Spring Boot 启动器中使用 WebSecurityConfigurerAdapter

标签 java spring spring-boot spring-security

我正在尝试通过定义从 WebSecurityConfigurerAdapter 扩展的配置类为我的自定义安全配置(LDAP + JWT)创建我自己的 spring boot starter。 .但是,当我使用这个启动器启动我的应用程序时,我得到:

IllegalStateException: Found WebSecurityConfigurerAdapter as well as SecurityFilterChain.
Please select just one.
我发现由于 this issue of the spring security 不可能再这样做了. spring的WebSecurityConfiguration中有一个断言:
Snapshot from WebSecurityConfiguration.java
我解决了这个问题 在启动器中添加以下内容(根据问题):
@Bean
@Order(1) //Explanation for this below
open fun filterChain(http: HttpSecurity, jwtHelper: JwtHelper): SecurityFilterChain {
    return http.authorizeRequests()
           ...
               .addFilterBefore(JwtAuthenticationFilter(jwtHelper), UsernamePasswordAuthenticationFilter::class.java)
               .and().build()
}

@Bean
open fun authenticationProvider(ldapConfig: LdapConfig): ActiveDirectoryLdapAuthenticationProvider {
    return ActiveDirectoryLdapAuthenticationProvider(...)
}
我加了 @Order(1)因为有两个 securityFilterChains :我的(在上面的配置中定义)和另一个来自未知来源的。我想,后者是无法使用 WebSecurityConfigurerAdapter 的原因.
主要问题是我找不到它来自哪里。

断点来自 WebSecurityConfiguration ... 以防万一:
Break point from WebSecurityConfiguration
我假设,因此我不能使用 @EnableGlobalMethodSecurity(prePostEnabled = true)以及。它说:
Cannot apply org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$EnableGlobalAuthenticationAutowiredConfigurer
to already built object

这是我的依赖项列表:
  • 带有启动器使用的模型的模块(名称:my-ldap-security-model):
  • dependencies {
        compileOnly 'org.springframework.security:spring-security-core'
        compileOnly 'org.springframework:spring-web'
        compileOnly 'javax.servlet:javax.servlet-api'
        api 'jakarta.xml.bind:jakarta.xml.bind-api'
        api 'org.glassfish.jaxb:jaxb-runtime'
        api 'io.jsonwebtoken:jjwt'
    }
    
  • 带有启动器使用的模型的模块(名称:my-ldap-security-spring-boot-starter):
  • dependencies {
        compile project(':my-ldap-security-model')
        compileOnly 'javax.servlet:javax.servlet-api'
        api 'org.springframework.security:spring-security-core'
        api 'org.springframework.security:spring-security-config'
        api 'org.springframework.security:spring-security-web'
        api 'org.springframework:spring-web'
        api 'org.springframework.security:spring-security-ldap'
    }
    
  • 应用项目:
  • dependencies {
        implementation('org.springframework.boot:spring-boot-starter-web')
        implementation('com.demo.boot:my-ldap-security-spring-boot-starter:0.0.1')
    }
    
    请帮我找出那个过滤器的根。

    最佳答案

    最初,默认 SecurityFilterChain如果有任何 WebSecurityConfigurerAdapter 将被禁用.但是,如果 spring 安全自动配置的优先级高于您的 WebSecurityConfigurerAdapter 的自动配置,则不起作用。 .
    解决方案 : 我加了@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)在自动配置类之上。不再有默认的安全过滤器链 :)
    关于 @EnableGlobalMethodSecurity ...这是关于缓存。突然,它得到了修复。

    关于java - 无法在自定义 Spring Boot 启动器中使用 WebSecurityConfigurerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66456025/

    相关文章:

    java - 当任务栏位置或大小发生变化时如何移动 JFrame?

    java - JDom 是否有任何 org.w3c.dom 包装器?

    java - 强制spring data rest使用https方案

    spring-boot - Spring Boot 项目 Jar 文件不读取放在类路径上的文件

    spring-boot - 带有@ConditionalOnProperty 的@RefreshScope 不起作用

    java - 如果将常量返回给 hashcode 并将 false 返回给 equals 会发生什么

    java - 如何检查 Apache FreeMarker 中该数组的大小 > 1?

    java - 如何向假客户端添加请求拦截器?

    java - 如何定义用于 <set></set> 的自定义 Set 实现

    spring-boot - 在 application.properties 文件中使用 tcp 端口 "9300"但仍然得到 "None of the configured nodes are available"