Spring mvc Interceptor 解决Session超时配置流程
最近公司内部框架中对Session超时这一功能未实现,由于采用iframe结构,Session超时后,当点击左侧系统菜单时,会在iframe的右侧再次弹出登陆框。
该问题是由于没有设置拦截器造成。
添加拦截器思路:当Session超时后,用户点击menu时,需要用Interceptor进行前项拦截,并判断此时session中是否还存在用户信息,如果不存在,将其指定登陆主页面。
如下代码:
1)首先在applicationContext-mvc.xml中加入mvc:interceptor标签。
<!-- session timeout interceptor -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*/*" />
<bean class="com.lenovo.lstp.mam.interceptor.SessionTimeoutInterceptor" >
<property name="allowUrls">
<list>
<value>/login/login.do</value>
<value>/common/language.do</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- exception handler -->
<bean id="handlerExceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
<property name="exceptionMappings">
<props>
<prop key="com.lenovo.lstp.mam.exception.SessionTimeoutException">/blank</prop>
</props>
</property>
</bean>
上述代码中首先要在系统内部包中创建一个名为SessionTimeoutInterceptor的拦截器,并指定允许的访问的url为list中集合。
当用户从此地址登陆后,无需进行拦截。
SessionTimeoutException中为当拦截生效后,会throw出该异常。
并进入blank.jsp页面。
2)第二步则要进行拦截器SessionTimeoutInterceptor创建,代码如下:
/**
* Session超时,拦截访问
*
*/
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String requestUrl = request.getRequestURI(); for(String url : allowUrls) {
if(requestUrl.endsWith(url)) {
return true;
}
} String session = (String) WebUtils.getSessionAttribute(request,
"username");
if(session != null) {
return true;
}else {
throw new SessionTimeoutException();
} }
除了被允许的Url,其他任何Url,只要没有检查到Session的存在,则会抛出SessionTimeoutException,用于去指向登陆页面,SessionTimeoutException中则无需写入任何操作。
3)由于iframe布局会造成登陆框内嵌问题,因此可以通过以下方式实现,代码如下:
var session = "${user}";
if("" == session){
top.location = "transfer.jsp";
}
if (null == session) {
top.location = "transfer.jsp";
}
在blank.jsp中引入一个中转页transfer.jsp。这个页面用于进行post跳转,再次去请求login.do。
top.location为指定在主页面展示,而不是在内嵌的页面展示。
4)第四步则需要进行二次login.do的请求,代码如下:
<script type="text/javascript">
$(document).ready(function(){
document.transfer.submit();
});
</script> <body>
<form name="transfer" action="login/login.do" method="post"></form> </body>
当进入该页面,会自动提交login.do请求,但是之前由于页面允许了login.do的进入,该操作可以在loginControll中进行判断。
代码如下:
/* After session timeout, check dto's username, and return login.jsp. */
if(dto.getUsername() == null) {
ModelAndView mv = new ModelAndView("login");
return mv;
}
在login方法中引入该判断 ,对二次访问该Controll的信息进行判断,如果用户名没有的话,则自动跳回login.jsp页面重新输入。
此时,已大功告成,如果登陆页面有其他链接,可以在allowurl去进行配置。
Spring mvc Interceptor 解决Session超时配置流程的更多相关文章
- 2017.3.31 spring mvc教程(二)核心流程及配置详解
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- 惊呆了,Servlet Filter和Spring MVC Interceptor的实现居然这么简单
前言 创建型:单例模式,工厂模式,建造者模式,原型模式 结构型:桥接模式,代理模式,装饰器模式,适配器模式,门面模式,组合模式,享元模式 行为型:观察者模式,模板模式,策略模式,责任链模式,状态模式, ...
- Spring MVC 中获取session的几种方法
Spring MVC 中使用session是一种常见的操作,但是大家上网搜索一下可以看到获取session的方式方法五花八门,最近,自己终结了一下,将获取session的方法记录下来,以便大家共同学习 ...
- Spring MVC、MyBatis整合文件配置详解
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
- Spring mvc interceptor配置拦截器,没有登录跳到登录页
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- [转]Spring mvc interceptor配置拦截器,没有登录跳到登录页
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.s ...
- Spring、Spring MVC、MyBatis整合文件配置详解
原文 http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...
- 【转】Spring、Spring MVC、MyBatis整合文件配置详解
见:http://www.tuicool.com/articles/eyINveF web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经支持注解配置方式 ...
随机推荐
- 详解浏览器缓存机制与Apache设置缓存
一.详解浏览器缓存机制 对于,如何说明缓存机制,在网络上找到了两张图,个人认为思路是比较清晰的.总结时,上图. 这里需要注意的有两点: 1.Last-Modified.Etag是响应头里的数据 2.I ...
- 169. Majority Element
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- Android EditText多行显示及所有属性
android:id="@+id/editSms" android:layout_width="fill_parent" android:layout_heig ...
- Emmet(前身是zen coding)介绍和使用
Zen coding - 一种快速编写HTML/CSS代码的方法.它使用仿CSS选择器的语法来快速开发HTML和CSS - 由Sergey Chikuyonok开发. 现在它改名为了Emmet,并且搭 ...
- dubbo spring2.5.6与spring 3冲突解决
dubbo的详细资料请参考: http://alibaba.github.io/dubbo-doc-static/Administrator+Guide-zh.htm#AdministratorGui ...
- 【HDOJ】1197 Specialized Four-Digit Numbers
水题,暴力可解. #include <iostream> using namespace std; int chg(int n, int base); int main() { int i ...
- PO > Create PO时关于汇率问题需要注意的步骤
为了使得RMB采购的PO在审核时不会提示汇率丢失(如下图),在创建PO时需要注意几个步骤. 1)手动创建PO:在建立PO行之前,应该选择好正确的"地点","币 ...
- Sublime Text 插件 autoprefixer
Sublime Text 早就有插件(Sublime Prefixr)使用 prefixr 的 API 来自动完成 CSS 前缀,但是 autoprefixer 更牛,这款可使用 Can I Use ...
- php.ini 干了些啥?
今天又重新看了一遍php.ini 的各种配置介绍,感觉还是官网说的比较靠谱,朋友,你所要找的,都在这里了. http://www.php.net/manual/zh/ini.core.php
- Xcode6 运行程序后,右侧Debug区域的Memory显示空白解决方法
http://chenyh-blog.com/%E8%9B%8B%E7%96%BC%E7%9A%84%E5%86%85%E5%AD%98-%E7%AC%AC%E4%B8%89%E7%AF%87-sdw ...