我需要在自定义过滤器中使用Spring Security的AuthenticationManagerBean,但它似乎总是给一个null。我正在使用1.3.8的弹簧引导。下面是从代码片段:自动装配春天AuthenticationManager过滤器总是给出空
@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会为空。
帮助表示赞赏。
您是否阅读过http://stackoverflow.com/questions/5417509/get-instance-of-authenticationmanager-manually或那一个http://stackoverflow.com/questions/21633555/how-to-inject-authenticationmanager -using-java-configuration-in-a-custom-filter? – Victor
你读过[link](http://stackoverflow.com/questions/21633555/how-to-inject-authenticationmanager-using-java-configuration-in-a-custom-filter)还是那个[Get instance of手动AuthenticationManager(http://stackoverflow.com/questions/5417509/get-instance-of-authenticationmanager-manually)? – Victor
你读过[link](http://stackoverflow.com/questions/21633555/how-to-inject-authenticationmanager-using-java-configuration-in-a-custom-filter)还是那个[Get instance of手动AuthenticationManager(http://stackoverflow.com/questions/5417509/get-instance-of-authenticationmanager-manually)? – Victor