Spring Security版本:5.5.1
最近学Spring Security,实现自定义的前后端分离的身份验证,发现通过继承AbstractAuthenticationProcessingFilter类实现的自定义过滤器在验证成功后,总是自动跳转“/”地址
查看AbstractAuthenticationProcessingFilter源码里的doFilter方法:
Authentication authenticationResult = this.attemptAuthentication(request, response);
if (authenticationResult == null) {
return;
}
this.sessionStrategy.onAuthentication(authenticationResult, request, response);
if (this.continueChainBeforeSuccessfulAuthentication) {
chain.doFilter(request, response);
}
this.successfulAuthentication(request, response, chain, authenticationResult);
执行了一个if,continueChainBeforeSuccessfulAuthentication值默认为false,所以并没有正常走到下一个过滤器反而是去执行了successfulAuthentication方法
在实现类中手动调用setContinueChainBeforeSuccessfulAuthentication置为true后正常执行,不进行跳转
public ImAuthenticationFilter(AuthenticationManager authenticationManager) {
super(new AntPathRequestMatcher("/**"));
this.authenticationManager = authenticationManager;
setContinueChainBeforeSuccessfulAuthentication(true);
}
估计是走了默认的验证成功行为,我也没有去定义成功行为
若自定义了成功行为,应该不要将continueChainBeforeSuccessfulAuthentication置true
但我没有试过