我正在用spring boot
2.7.4
版本编写应用程序,它有新版本的spring security
。所以我需要把我的旧代码重写成新的。下面是我使用WebSecurityConfigurerAdapter
的旧安全配置
@Configuration
@EnableWebSecurity
class AppWebConfig(
val customUserDetailsService: CustomUserDetailsService,
val passwordEncoder: PasswordEncoder,
val tokensService: TokensService
) : WebSecurityConfigurerAdapter() {
@Throws(Exception::class)
override fun configure(auth: AuthenticationManagerBuilder) {
auth
.userDetailsService(customUserDetailsService)
.passwordEncoder(passwordEncoder)
}
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
http
.cors()
.and()
.csrf().disable()
.exceptionHandling()
//urls permissions...
.addFilter(AppAuthorizationFilter(authenticationManager(), tokensServicee))
}
}
这是新代码
@Configuration
@EnableWebSecurity
class AppWebConfig(
val tokensService: TokensService,
) {
@Bean
@Throws(Exception::class)
fun authenticationManager(authenticationConfiguration: AuthenticationConfiguration): AuthenticationManager? {
return authenticationConfiguration.authenticationManager
}
@Bean
@Throws(Exception::class)
protected fun fitlerChain(http: HttpSecurity): SecurityFilterChain {
val authenticationManager = http.getSharedObject(AuthenticationManager::class.java)
return http
.cors()
.and()
.csrf().disable()
//urls permissions...
.addFilter(AppAuthorizationFilter(authenticationManager, tokensService))
.build()
}
下面是在两个版本中都没有更改的AppAuthorizationFilter
,以及使用authenticationManager
的地方:
class AppAuthorizationFilter(
authenticationManager: AuthenticationManager,
tokensService: TokensService,
) : BasicAuthenticationFilter(authenticationManager) {
private val tokensService: TokensService
init { this.tokensService = tokensService }
@Throws(IOException::class, ServletException::class)
override fun doFilterInternal(
request: HttpServletRequest,
response: HttpServletResponse,
chain: FilterChain,
) {
val header = request.getHeader(Objects.requireNonNull(HttpHeaders.AUTHORIZATION))
if (header != null) {
val authorizedUser = tokensService.parseAccessToken(header)
SecurityContextHolder.getContext().authentication = authorizedUser
}
chain.doFilter(request, response)
}
}
问题是来自authenticationManager
的AppWebConfig
。我得到的错误是这是空的。
Caused by: java.lang.NullPointerException: authenticationManager must not be null at com.app.security.config.WebConfig.fitlerChain(WebConfig.kt:68)
我尝试了我前面展示的解决方案,方法是从共享对象获取authenticationManager
,http.getSharedObject(AuthenticationManager::class.java)
,但是它不能像您看到的那样工作。
我通过从authenticationManager
获得WebApplicationContext
来解决这个问题,但我不确定这是否是最好的方法
val authenticationManager = applicationContext.getBean("authenticationManager") as AuthenticationManager
发布于 2022-11-03 08:26:14
尝试将这个注释(@Primary)
添加到authenticationManager bean中。如果不起作用,那就按照你提到的解决方案去做。
@Bean
@Primary // try to add this
@Throws(Exception::class)
fun authenticationManager(authenticationConfiguration: AuthenticationConfiguration): AuthenticationManager? {
return authenticationConfiguration.authenticationManager
}
https://stackoverflow.com/questions/74296028
复制相似问题