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';
                }
            }
        });

 

转载于:https://www.cnblogs.com/damour-damocles/p/4989113.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值