最近在用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';
}
}
});

最新文章

  1. oracle去除重复字段
  2. CI生成查询记录集result(),row(),row_array().....
  3. WCF service 获取 client 端的 IP 和 port (转)
  4. lucene-查询query-&gt;BooleanQuery “与或”搜索
  5. [linux]ubuntu 下安装RMySQL包
  6. AT&amp;T Assembly for Linux and Mac (sys_write)
  7. [linux]删除目录下的一类文件
  8. centos 6.4/redhat 6.4 安装gitlab
  9. HDU 4604 Deque 最长子序列
  10. http协议、模块、express框架以及路由器、中间件和mysql模块
  11. 小米5如何支持AT&amp;T网络运营商
  12. 1、IDEA的常用快捷键
  13. Dapper 封装oracle底层访问数据库
  14. 基于CentOS安装FTP服务器
  15. 通俗易懂理解Linux文件权限修改chmod命令
  16. day33 网络编程之线程,并发以及selectors模块io多路复用
  17. Linux内核剖析(三)构建源码树
  18. web新手——新闻列表这样写不容易出错
  19. chattr和lsattr命令的使用(对于root用户也无法修改删除的操作问题)
  20. 设计模式 笔记 原型模式 prototype

热门文章

  1. SpringMVC POJO传参方式
  2. Apache服务器配置https
  3. mysql的视图、索引、触发器、存储过程
  4. UVA-10480-Sabotage(最大流最小割,打印路径)
  5. C#.Net集成Bartender条码打印,VS调试运行可以打印,发布到IIS运行打印报错
  6. 018:include函数详解
  7. 185.[USACO Oct08] 挖水井 (第三次考试大整理)
  8. nginx 日志文件分隔
  9. struts2.3.20+spring4.0.2+hibernate4.3.4框架整合
  10. 组件内导航之beforeRouteUpdate的使用