You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在自定义Filter中注入Authentication Manager?

Spring Security自定义Filter注入AuthenticationManager的解决方案

问题场景

在Spring Security开发过程中,自定义CustomFilter时需要注入AuthenticationManager,原代码行http.addFilter(new CustomAuthenticationFilter(authenticationManager(ERROR)));无法正确获取实例,不清楚如何通过AuthenticationConfiguration创建AuthenticationManager。

此前尝试的问题

之前在WebMvcConfigurerAdapter中可以通过重写authenticationManagerBean()方法获取AuthenticationManager,但在WebSecurityConfig中直接注入时出现循环依赖问题。

最终解决代码

通过HttpSecurity的getSharedObject方法获取AuthenticationConfiguration实例,再创建AuthenticationManager,正确的代码如下:

http.addFilter(new CustomAuthenticationFilter(authenticationManager(http.getSharedObject(AuthenticationConfiguration.class))));

内容的提问来源于stack exchange,提问作者Witkong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:01:08