首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在升级spring安全性之后,身份验证管理器为空。

在升级spring安全性之后,身份验证管理器为空。
EN

Stack Overflow用户
提问于 2022-11-02 21:55:43
回答 1查看 72关注 0票数 3

我正在用spring boot 2.7.4版本编写应用程序,它有新版本的spring security。所以我需要把我的旧代码重写成新的。下面是我使用WebSecurityConfigurerAdapter的旧安全配置

代码语言:javascript
运行
复制
@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))
    }

}

这是新代码

代码语言:javascript
运行
复制
@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的地方:

代码语言:javascript
运行
复制
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)
    }
}

问题是来自authenticationManagerAppWebConfig。我得到的错误是这是空的。

代码语言:javascript
运行
复制
Caused by: java.lang.NullPointerException: authenticationManager must not be null at com.app.security.config.WebConfig.fitlerChain(WebConfig.kt:68)

我尝试了我前面展示的解决方案,方法是从共享对象获取authenticationManagerhttp.getSharedObject(AuthenticationManager::class.java),但是它不能像您看到的那样工作。

我通过从authenticationManager获得WebApplicationContext来解决这个问题,但我不确定这是否是最好的方法

代码语言:javascript
运行
复制
val authenticationManager = applicationContext.getBean("authenticationManager") as AuthenticationManager
EN

回答 1

Stack Overflow用户

发布于 2022-11-03 08:26:14

尝试将这个注释(@Primary)添加到authenticationManager bean中。如果不起作用,那就按照你提到的解决方案去做。

代码语言:javascript
运行
复制
@Bean
@Primary // try to add this
@Throws(Exception::class)
fun authenticationManager(authenticationConfiguration: AuthenticationConfiguration): AuthenticationManager? {
    return authenticationConfiguration.authenticationManager
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74296028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档