【发布时间】:2021-06-01 23:28:55
【问题描述】:
我正在尝试通过定义从WebSecurityConfigurerAdapter 扩展的配置类,为我的自定义安全配置(LDAP + JWT)创建自己的 Spring Boot 启动器。但是,当我使用这个启动器启动我的应用程序时,我得到:
IllegalStateException: Found WebSecurityConfigurerAdapter as well as SecurityFilterChain.
Please select just one.
由于this issue of the spring security,我发现不再可能这样做。 spring的WebSecurityConfiguration中有一个断言:
我解决了这个问题在启动器中添加了以下内容(根据问题):
@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的断点...以防万一:
我假设,因此我不能使用@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')
}
请帮我找出那个过滤器的根。
【问题讨论】:
-
它的 spring boot 自动配置 docs.spring.io/spring-security/site/docs/current/reference/… 我也建议你把 jjwt 换成 docs.spring.io/spring-security/site/docs/current/reference/… 因为 spring 默认支持 JOSE。
-
spring boot 还默认支持 jwt 并内置了 jwt 过滤器,因此无需注册您自己的自定义 jwt 过滤器。 docs.spring.io/spring-security/site/docs/current/reference/… 这意味着如果你想使用 JWT,你需要做的就是注册一些 beans,然后每个应用程序可以定义他们想要 JWT 身份验证的端点,他们就会得到你的 bean。无需注册过滤器并构建自定义过滤器链。
标签: java spring spring-boot spring-security