zoukankan      html  css  js  c++  java
  • 【struts2】自定义登录检查拦截器

      在实际开发中,一个常见的功能要求是:有很多操作都需要登录后才能操作,如果操作的时候还没有登录,那么通常情况下会要求跳转回到登录页面。

      1)如何实现这样的功能呢?

      在具体实现之前,先来考虑几个问题:

      (1)这个功能应该在哪里实现?

      要实现登录检查的功能,很明显是在Action运行之前,就要判断用户是否登陆了,判断方式是看session里面是否有相关信息,如果有,则继续操作;如果没有,则要跳转到预先制定好的登录页面。简单点说,登录检查应该写在“invocation.invoke();”语句之前。

      (2)是否需要参数?

      要判断是否需要参数,其实就是判断这个拦截器有没有什么可变的量,可以把这些可变量从程序中分离出来,通过struts.xml来配置。经过分析,可以抽出两个参数:

    • 代表登陆页面的Result
    • 判断session中哪个attribute,也就是attribute的名称

      (3)如何引用呢?

      现在的情况是只有部分Action需要登陆录检查,另外一些Action不需要,这就需要权衡了。对于大多数Action都要进行登录检查的包,可以在包的默认拦截器引用上设置登录检查,而对于少数不需要登陆检查的Action,可以让它们直接引用默认的defaultStack拦截器栈。

      2)实现满足要求的拦截器,示例代码如下:

    package cn.javass.hello.struts2impl.action;
    
    import java.util.Map;   
    import com.opensymphony.xwork2.ActionInvocation;  
    import com.opensymphony.xwork2.interceptor.Interceptor;  
      
    public class SessionCheckInterceptor implements Interceptor{  
        //设置参数  
        private String sessionAttribute;  
        private String reloginResult;  
        public void setSessionAttribute(String sessionAttribute) {  
            this.sessionAttribute = sessionAttribute;  
        }  
        public void setReloginResult(String reloginResult) {  
            this.reloginResult = reloginResult;  
        }  
        public void destroy() { }  
        public void init() {    }  
        public String intercept(ActionInvocation invocation) throws Exception {  
            //读取Session  
            Map<String, Object> session = invocation.getInvocationContext().getSession();  
            //判断Session中是否有相应的attribute  
            if (session.containsKey(sessionAttribute)){  
                String resultCode = invocation.invoke();  
                return resultCode;  
            }else{  
                return reloginResult;  
            }         
        }  
    }  

      在intercept方法中,先读取session中指定的attribute,具体读取哪个attribute由参数从外界传进来,然后判断Session中是否存在这个attribute,如果有则继续执行后续的拦截器、Action和Result,如果没有则跳转到指定的Result所对应的页面,具体跳转到哪个Result也是由参数从外界传进来的。

      3)拦截器配置,配置示例如下:

    <package name="helloworld"  extends="struts-default">  
            <interceptors>  
                <interceptor name="testInteceptor" class="cn.javass.hello.struts2impl.action.MyInterceptor"/> 
                <interceptor name="myLogger" class="cn.javass.hello.struts2impl.action.LoggerInterceptor"/>
                <interceptor name="loginChecker" class="cn.javass.hello.struts2impl.action.SessionCheckInterceptor"/>    
                <interceptor-stack name="myStack">  
                    <interceptor-ref name="timer"/>  
                    <interceptor-ref name="testInteceptor"/>
                    <interceptor-ref name="myLogger"/>  
                    <interceptor-ref name="loginChecker">  
                        <param name="sessionAttribute">login_user</param>  
                        <param name="reloginResult">login</param>  
                    </interceptor-ref>  
                    <interceptor-ref name="defaultStack"/>  
                </interceptor-stack>  
            </interceptors>  
            <default-interceptor-ref name="myStack"/>  
            
            <global-results>  
                <result name="math-exception">/${folder}/error.jsp</result> 
                <result name="login">/s2impl/login.jsp</result> 
            </global-results>  
            <global-exception-mappings>  
                <exception-mapping result="math-exception" exception="java.lang.ArithmeticException"/>  
                <exception-mapping result="math-exception" exception="java.lang.Exception"/>  
            </global-exception-mappings>  
            
            <action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction"> 
                <result name="toWelcome">/${folder}/welcome.jsp</result>   
            </action>  
        </package>  

      参考资料:http://www.iteye.com/topic/1124526

  • 相关阅读:
    tp5怎么验证手机号码
    awk理论详解、实战
    chpasswd、dd命令、find实战、添加系统服务、buffer、cached
    python中执行shell命令的几个方法
    rsync使用详解
    sort、dirname、添加环境变量、修改主机名、别名IP、静态路由
    awk数组详解、实战
    python使用Queue进行进程间通信
    python生成器、迭代器、__call__、闭包简单说明
    python装饰器的深度探究
  • 原文地址:https://www.cnblogs.com/ningvsban/p/3735754.html
Copyright © 2011-2022 走看看