最近在开发一个政务方面的项目,系统框架用到了spring security,结果出现了一个问题:就是session过期后页面跳转至登录页面的问题;
这里要分两种情况了;
第一种:普通的请求,如form表单提交,重定向等
spring-security.xml配置如下:
<session-management invalid-session-url="http://172.31.60.117:8088/mslogin/view/login.jsp" session-fixation-protection="newSession" > <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" /> </session-management> -
web.xml配置如下:
<listener> <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> </listener>
于是就可以监听到session后期后普通请求的拦截,跳转至登录页面。结果后期测试发现ajax请求无法拦截至登录页面,这时候的现象为ajax请求发出后无法请求到数据,但是页面又不能跳转,于是代码改为下面这种,既可以支持普通请求拦截,又可以支持ajax请求拦截;
spring-security.xml 配置如下
<session-management invalid-session-url="/public/invalidate" session-fixation-protection="newSession" > <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" /> </session-management>
这里/public/invalidate为Controller请求路径:创建Controller
package com.jointem.ngcms.controller; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @Title: InvalidateSession * @Description: * @Author: bql * @Date: 2016年4月20日 * @Version: v1.0.0 * @Copyright: 2016 www.jointem.com Inc. All rights reserved. * @Update: 1. Create File */ @Controller public class InvalidateSession { /** * This url gets invoked when spring security invalidates session (ie timeout). * Specific content indicates ui layer that session has been invalidated and page should be redirected to logout. */ @RequestMapping(value = "/public/invalidate") @ResponseBody public String invalidateSession(HttpServletRequest reqeust,HttpServletResponse response) { String ajaxHeader = reqeust.getHeader("X-Requested-With"); boolean isAjax = "XMLHttpRequest".equals(ajaxHeader); if (isAjax) { return "invalidSession"; } else { try { response.sendRedirect("http://172.31.60.117:8088/mslogin/view/login.jsp"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return ""; } }
注意这里的几行代码为判断是否是ajax请求。
String ajaxHeader = reqeust.getHeader("X-Requested-With"); boolean isAjax = "XMLHttpRequest".equals(ajaxHeader);
如果是ajax请求,则返回invalidSession字符串,在前端js中添加如下代码就可以监听到后端的返回值
//全局的ajax访问,处理ajax清求时sesion超时 $.ajaxSetup({ complete: function(xhr, status) { if (xhr.responseText == 'invalidSession') { window.location = "http://172.31.60.117:8088/mslogin/view/login.jsp"; } } });
$.ajaxSetup为全局监听ajax请求,当后端返回值为invalidSession就可以跳转至自己需要的页面了。
在google 上搜了好半天,到最后也没解决掉问题,于是就自己改造为以上提供的代码,希望能帮助到遇到和我同样问题的小伙伴。
相关推荐
Springboot+SpringSecurity+SpringSession+Redis+Mybatis-Plus+Swwager
spring-security多登录页面配置,包括前台和后台分开登录界面,注销登录返回不同的界面等。
spring-security多个登录页面配置,分开了前台登录页面和后台登录页面登录,还有注销返回的不同的界面。
参考资料: http://docs.spring.io/spring-session/docs/current-SNAPSHOT/reference/html5/guides/security.html http://blog.csdn.net/xiejx618/article/details/43059683
Spring_Security_多页面登录配置教程
session失效重新登录后,返回之前页面,并保留表单数据
SpringSecurity自定义登陆页面,当登陆失败后,需要在页面上回显错误信息,但是错误信息又是由SpringSecurity自己封装的,我们如何去获取并且如何将错误信息转换成中文显示在页面上,本资源对其进行了详情的解决,...
1.本项目为SpringCloud Gateway的微服务框架,整合了SpringSecurity,微服务间使用Redis来获取登陆的用户信息。 2.由于Gat
包含翻译后的API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.springframework.session:spring-session-data-redis:2.0.4.RELEASE; 标签:spring...
springboot+ spring security实现登录认证
主要介绍了Spring Security OAuth2集成短信验证码登录以及第三方登录,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
项目中使用到的技术包含SpringBoot、SpringSecurity&oauth2(安全资源和授权中心模式、包括登录接口自定义返回字段、自定义手机号+密码登录、自定义免密登录)、Queue队列、线程池、xss攻击配置、SpringCache、Mybatis...
包含翻译后的API文档:spring-session-core-2.0.5.RELEASE-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.springframework.session:spring-session-core:2.0.5.RELEASE; 标签:springframework、...
NULL 博文链接:https://tonytony3.iteye.com/blog/1214081
在session共享中遇到的坑。自己通过更改源码实现自定义功能
实现会话控制,权限控制,免登陆的spring security完整项目 博文链接:https://abc08010051.iteye.com/blog/1995886
Spring Security 演讲PPT(演讲嘉宾:张明星) WebSphere技术专家沙龙在广州圆满举办,WSC超级版主Fastzch(张明星)担任本次沙龙的演讲嘉宾,他给广州的WebSphere技术专家带来了以“Spring Security ”为主题的...
运行SpringBootMainClass启动后,请在地址栏访问http://ip:port,在页面中登录,成功后会跳转至下一个页面,此时需要后退到刚才的页面,点击各个超链接来试验用户的授权情况。未登录时点击任何链接都会跳回首页。 ...
Spring Security三份资料,实战Spring Security 3.x.pdf;Spring Security 3.pdf;Spring Security使用手册.pdf
包含翻译后的API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.springframework.session:spring-session-data-redis:2.0.4.RELEASE; 标签:springframework、...