Spring Security继承AbstractAuthenticationProcessingFilter验证成功后自动跳转地址“/”

在学习SpringSecurity时,博主遇到一个问题:自定义的前后端分离身份验证过滤器在验证成功后总是自动跳转到'/'。经过分析AbstractAuthenticationProcessingFilter源码,发现默认行为在验证成功后会继续执行successulAuthentication方法导致跳转。通过设置`setContinueChainBeforeSuccessfulAuthentication(true)`,使得过滤器能够正常执行后续操作,避免了不必要的跳转。这表明在未定义自定义成功行为时,需注意该配置以控制过滤器行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
但我没有试过

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值