繁体   English   中英

无法在自定义 spring 引导启动器中使用 WebSecurityConfigurerAdapter

[英]Can't use WebSecurityConfigurerAdapter in a custom spring boot starter

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

IllegalStateException: Found WebSecurityConfigurerAdapter as well as SecurityFilterChain.
Please select just one.

我发现由于spring security 的这个问题,不再可能这样做。 spring 的 WebSecurityConfiguration 中有一个断言:

来自 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的断点...以防万一: WebSecurityConfiguration 的断点

我假设,因此我也不能使用@EnableGlobalMethodSecurity(prePostEnabled = true) 它说:

Cannot apply org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$EnableGlobalAuthenticationAutowiredConfigurer
to already built object

这是我的依赖项列表:

  1. 启动器使用的带有模型的模块(名称: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'
}
  1. 带有启动器使用的模型的模块(名称: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'
}
  1. 应用项目:
dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('com.demo.boot:my-ldap-security-spring-boot-starter:0.0.1')
}

请帮我找出那个过滤器的根。

最初,如果有任何WebSecurityConfigurerAdapter ,默认的SecurityFilterChain将被禁用。 但是,如果 spring 安全自动配置的优先级高于WebSecurityConfigurerAdapter的自动配置,则它不起作用。

解决方案:我在自动配置 class 上方添加了@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) 不再有默认的安全过滤器链:)

关于@EnableGlobalMethodSecurity ...这是关于缓存的。 突然,它得到了修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM