【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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值