java重定向session失效_SpringSecurity Session并发过期后会重定向到 /login (入口点问题)问题的解决...

本文主要探讨了在SpringSecurity中遇到的session并发控制问题,当maximumSessions设置为1时,超出限制的登录会导致先前登录的用户被挤下线。作者通过调试发现,异常发生在`SecurityContextHolder.getContext().getAuthentication()`返回null时,导致`AuthenticationCredentialsNotFoundException`异常。进一步研究发现,这是`ExceptionTranslationFilter`处理AuthenticationException的结果,它默认将用户重定向到/login。为解决此问题,作者自定义了`AuthenticationEntryPoint`和`AccessDeniedHandler`,返回HTTP 405状态码,避免了重定向到/login的情况。

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

问题描述

在 SpringSecurity 中,我想配置一个关于session并发的控制,于是我是这样配置的

@Override

protected void configure(HttpSecurity http) throws Exception {

http

.sessionManagement()

.invalidSessionStrategy(new InvalidSessionStrategyImpl())

.maximumSessions(-1).expiredSessionStrategy(expiredSessionStrategy())//配置并发登录,-1表示不限制

.sessionRegistry(sessionRegistry());

}

上下文的配置我在此省略了

这里设置 maximumSessions 为 -1,表示不限制同一账号登录的客户端数

session过期后执行的逻辑是进入我自定义的类 expiredSessionStrategy() 中

因为我是构建的 rest 服务,所以我是返回的 http 状态码

public class ExpiredSessionStrategyImpl implements SessionInformationExpiredStrategy {

@Override

public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException {

event.getResponse().sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED,

JSONObject.toJSONString(MessageBody.failure(405,"not login or login has been expired")));

}

}

在这里,问题就来了

我测试的时候,把 -1 改成了 1,之后登录同一个用户,后面登录的用户会把前面一个已经登录的用户挤下线,就是说之前登录的那个用户的session 会过期

就是说他所在的页面再发送任何请求的话会收到我返回的 405 状态码

在这里是没问题的

问题就在发完一个请求后,在发一个请求,在浏览器的 network 上会看到发出的请求会被重定向的 /login 请求上

后续再发任何请求都会被重定向到 /login 上

问题思考

为什么会出现这样的情况呢?

为什么会第一个请求会收到405的状态码,后续的请求会被重定向到 /login 呢?

通过 debug 断点,我定位到过滤器的前置执行方法 beforeInvocation() 上

protected InterceptorStatusToken beforeInvocation(Object object) {

Assert.notNull(object, "Object was null");

final boolean debug = logger.isDebugEnabled();

if (!getSecureObjectClass().isAssignableFrom(object.getClass())) {

throw new IllegalArgumentException(

"Security invocation attempted for object "

+ object.getClass().getName()

+ " but AbstractSecurityInterceptor only configured to support secure objects of type: "

+ getSecureObjectClass());

}

Collection attributes = this.obtainSecurityMetadataSource()

.getAttributes(object);

if (attributes == null || attributes.isEmpt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值