【发布时间】:2017-04-29 02:07:06
【问题描述】:
我需要在自定义过滤器中使用 Spring Security 的 AuthenticationManagerBean,但它似乎总是给出一个空值。我正在使用弹簧靴 1.3.8。这是代码中的一个sn-p:
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
}
过滤代码:
public class MyFilter extends OncePerRequestFilter {
private TokenExtractor tokenExtractor = new BearerTokenExtractor();
@Autowired
private AuthenticationManager authenticationManager;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
//some checks
Authentication receivedAuthentication = tokenExtractor.extract(request);
receivedAuthentication.setAuthenticated(true);
authenticationManager.authenticate(receivedAuthentication);
SecurityContextHolder.getContext().setAuthentication(receivedAuthentication);
}
}
}
由于 authenticationManager 为空而引发异常。
感谢您的帮助。
【问题讨论】:
-
是的,我也试过了,仍然为空
标签: java spring spring-boot spring-security