Spring Security Session Time Out
最近在用Spring Security做登录管理,登陆成功后,页面长时间无操作,超过session的有效期后,再次点击页面操作,页面无反应,需重新登录后才可正常使用系统。
为了优化用户体验,使得在session失效后,用户点击页面对服务器发起请求时,页面能够自动跳转到登录页面。本次使用spring security 3.1。
第一步:配置spring security的专用配置文件spring-security.xml。
<http auto-config="true" entry-point-ref="myLoginUrlAuthenticationEntryPoint"></http> <beans:bean id="myLoginUrlAuthenticationEntryPoint" class="com.ushareit.beyla.security.MyLoginUrlAuthenticationEntryPoint"> <beans:property name="loginFormUrl" value="/login.jsp"/> </beans:bean>
entry-point-ref属性,英文的意思是入口点引用,它其实是被ExceptionTranslationFilter引用的,该过滤器的作用是异常翻译。在出现认证异常、访问异常的时候,通过入口点决定redirect、forword的操作。比如现在是form-login的认证方式,如果没有通过UsernamePasswordAuthenticationFilter的认证就直接访问某个被保护的url,那么经过ExceptionTranslationFilter过滤器处理后,先捕获到访问拒绝异常,并把跳转动作交给入口点来处理。form-login的对应入口点类为LoginUrlAuthenticationEntryPoint,这个入口点类的commence方法会redirect或forward到指定的url(form-login标签的login-page属性)。
第二步:自定义MyLoginUrlAuthenticationEntryPoint继承LoginUrlAuthenticationEntryPoint类,并覆盖commence方法。
package com.ushareit.beyla.security; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; @SuppressWarnings("deprecation") public class MyLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest)request; if ("XMLHttpRequest".equalsIgnoreCase(httpRequest.getHeader("X-Requested-With"))){ response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"SessionTimeout"); } else{ super.commence(request, response, authException); } } }
由于只有在ajax请求的时候,其请求头中有“X-Requested-With”属性,而传统请求的时候,请求头中无此属性,因此针对ajax请求异常的时候,我们可以通过response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"SessionTimeout");来返回错误代码“401”,来标记访问出错。spring security在通过用户名和密码进行登录的时候是普通请求,直接通过super.commence(request, response, authException)
第三步:ajax方法中通过利用statusCode对象根据服务器返回的不同状态进行处理,我使用的jQuery。在session失效后,页面又发起的新的ajax请求中通过statusCode对象进行相应处理。
$.ajax({ url: 'xxxx', type: 'get', data: datas, cache: true, dataType: 'json', success: function (data) { alert(123); }, error: function (data) { console.log(data); }, statusCode: { 401: function() { alert("The session is timed out,please log in again!"); window.location.href = '/login.jsp'; } } });
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)