`
huangronaldo
  • 浏览: 220578 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

简单的struts2拦截器利用session拦截用户登录

阅读更多

本人在实习项目时,在网上找到一个简单的拦截用户登录,总结一下:希望高手指点,继续进步和完善。

首先编写拦截类LoginedCheckInterceptor.java 如下:package com.huangt.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.huangt.bean.SystemUser;
/* session过期、登录有效性及操作的权限验证拦截器 */
@SuppressWarnings("serial")
public class LoginedCheckInterceptor extends AbstractInterceptor {
    /** 拦截请求并进行登录有效性验证 */
    public String intercept(ActionInvocation ai) throws Exception {
        //取得请求的URL
        String url = ServletActionContext.getRequest().getRequestURL().toString();
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setHeader("Pragma","No-cache");          
        response.setHeader("Cache-Control","no-cache");   
        response.setHeader("Cache-Control", "no-store");   
        response.setDateHeader("Expires",0);
        SystemUser systemUser = null;
        //对登录与注销请求直接放行,不予拦截
        if (url.indexOf("login.action")!=-1 || url.indexOf("logout.action")!=-1){
            return ai.invoke();
        }
        else{
            //验证Session是否过期
            if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){
                //session过期,转向session过期提示页,最终跳转至登录页面
                return "tologin";
            }
            else{
                systemUser = (SystemUser)ServletActionContext.getRequest().getSession().getAttribute("systemUser");
                //验证是否已经登录
                if (systemUser==null){
                    //尚未登录,跳转至登录页面
                    return "tologin";
                }else{                    
                    return ai.invoke();
                                
                }                
            }            
        }
    }
}

 

然后用户登陆成功后,设置session的值。在LoginAction中登陆成功后加入一行:

 

ActionContext.getContext().getSession().put("systemUser",systemUser);
 

 

<!--EndFragment--> <!---->

最为重要的便是struts.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="struts2" extends="struts-default">
		
		 <!-- 配置自定义拦截器LoginedCheckInterceptor -->
        <interceptors>
            <interceptor name="loginedCheck" class="com.zjsoft.interceptor.LoginedCheckInterceptor"/>
            <interceptor-stack name="mystack">
                <interceptor-ref name="loginedCheck" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>        
    
        <!-- 定义全局result -->
        <global-results>
            <!-- 定义名为exception的全局result -->
            <result name="exception">/exception.jsp</result>
            <result name="tologin" type="redirect">/unlogind.jsp</result>
        </global-results>
        <!-- 定义全局异常映射 -->
        <global-exception-mappings>
            <!-- 捕捉到Exception异常(所有异常)时跳转到exception所命名的视图上 -->
            <exception-mapping exception="java.lang.Exception" result="exception"/>
        </global-exception-mappings>    

<!-- 用户登录 -->
		<action name="login" class="loginAction" >           
            <result name="input" >/login.jsp</result> 
            <result name="error" >/login.jsp</result> 
            <result name="success" type="redirect">/index.jsp</result>
        </action>
        <action name="logout" class="com.zjsoft.action.LogoutAction" >           
            <result name="success" type="redirect">/login.jsp</result>
        </action>
<!-- 系统用户信息管理 -->
		<action name="listSystemUser" class="systemUserAction" method="listSystemUser">
			<result name="success">/listSystemUser.jsp</result>
			<interceptor-ref name="mystack" />
		</action>

	</package>
</struts>

 在每个需要拦截登录的action中加入<interceptor-ref name="mystack" /> 便可。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics