1. 程式人生 > >Struts2的Session超時返回登入頁面

Struts2的Session超時返回登入頁面

今天做一個S2SM的專案時,在session超時或者失效時返回到登陸頁面重新登陸,前臺使用的easyui框架

1、首先修改web.xml

  <session-config>

   <session-timeout>1</session-timeout>

  </session-config>

2、自定義一個登入超時的攔截器

package com.tungkong.util;

import java.util.Hashtable;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")

public class LoginedCheckInterceptor extends AbstractInterceptor {

/** session過期、登入有效性及操作的許可權驗證攔截器 */

@SuppressWarnings("unchecked")

@Override

public String intercept(ActionInvocation ai) throws

 Exception {

  //取得請求的URL  

String url = ServletActionContext.getRequest().getRequestURL().toString();

HttpServletResponse response = ServletActionContext.getResponse();

response.setHeader("Pragma""No-cache");

response.setDateHeader("Expires", 0);

response.setHeader("Cache-Control""no-cache"

);

response.setHeader("Cache-Control""no-store");

response.setHeader("Content-Type""text/html; charset=UTF-8");

response.setHeader("Cache-Control""no-store");

response.setHeader("Content-Type""text/html; charset=UTF-8");

Hashtable<String, Object> userinfo = null;

//對登入與登出請求直接放行,不予攔截

if (url.indexOf("loginAction") != -1 || url.indexOf("logoutAction") != -1) {

return ai.invoke();

else {

//驗證Session是否過期

if (!ServletActionContext.getRequest().isRequestedSessionIdValid()) {

//session過期,轉向session過期提示頁,最終跳轉至登入頁面

return "tologin";

else {

//驗證是否已經登入

userinfo = (Hashtable<String, Object>) ServletActionContext.getRequest().getSession().getAttribute("userSessionHt");

if (userinfo == null) {

 //尚未登入,跳轉至登入頁面

return "tologin";

else {

return ai.invoke();

}

}

}

}

}

3、在struts.xml中配置攔截器,並宣告一個全域性的result,當session失效的時候攔截器會轉發到登陸頁面,由於我使用的是多個struts檔案,故應該這麼設定

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<!-- 載入預設的 struts2 配置檔案 -->

 <include file="struts-default.xml" />

 <!-- 業務模組配置 -->

<include file="struts-userinfo.xml" />

……………… 

<constant name="struts.i18n.encoding" value="UTF-8" />

<constant name="struts.objectFactory" value="spring"></constant>

<package name="tksm" extends="struts-default">

<interceptors>

 <interceptorname="loginedCheck"class="com.tungkong.util.LoginedCheckInterceptor"/>

 <interceptor-stack name="mystack">

  <interceptor-ref name="loginedCheck" />

  <interceptor-ref name="defaultStack" />

</interceptor-stack>

</interceptors>

 <global-results>

<result name="exception">/exception.jsp</result>

<result name="tologin">/relogin.jsp</result>

</global-results>

<global-exception-mappings>

<exception-mapping exception="java.lang.Exception" result="exception" />

</global-exception-mappings>    

</package>

</struts>  

4、在struts-userinfo.xml中設定

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="tksm-user" extends="tksm">

<default-interceptor-ref name="mystack" />

        <action name="loginAction" class="userAction" method="login">

            <result name="success">/WEB-INF/index/main.jsp</result>

            <result name="error">/WEB-INF/index/error.jsp</result>

        </action>

……………………

    </package>

</struts>

5、新建relogin.jsp頁面

由於頁面巢狀在iframe下,跳轉時需要跳轉到其父頁面,因此加個中間的jsp,攔截器配置跳轉到此頁面,再由此頁面跳轉到登入頁面。

relogin.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

  </head>

  <body>

    <script type="text/javascript">

     alert("對不起!您長時間對系統未進行操作,登入已超時,請重新登入!");

     window.top.location.href="<%=basePath%>login.jsp";

    </script>

  </body>

</html>

當我們登陸後一分鐘不做任何操作重新整理後則會跳轉到登陸頁面

這篇部落格主要來源於:

http://blog.csdn.net/java_cxrs/article/details/5519743

http://blog.sina.com.cn/s/blog_a72f208a01014gha.html

感謝兩位!