用户登陆拦截器的实现

来源:互联网 发布:科幻电影推荐知乎 编辑:程序博客网 时间:2024/04/30 02:52
--------------------------------------------------------------------------------
用户登陆拦截器的实现(AuthorityInterceptor):
1.先在struts-xml中定义一个拦截器,把名字和类名匹配好:

<package name="struts2" extends="struts-default">    <interceptors>        <interceptor name="authority" class="com.struts2.interceptor.AuthorityInterceptor">            <param name="excludeMethods">login,register</param>        </interceptor>    </interceptors></package>



2.拦截器定义好后,接下来就要实现这个拦截器的类,等于你告诉了struts要拦截,但没说要拦截什么,拦截的内容就在你定义的类中实现,拦截器都要继承MethodFilterInterceptor,重写其中的doIntercept方法就可以了,在函数中,检查请求的session中用户的信息,在登陆时,都要把用户的信息保存在sesion中,只要判断session中的用户信息是否存在就行了,代码如下:
@Overrideprotected String doIntercept(ActionInvocation invocation) throws Exception {    if (invocation.getInvocationContext().getSession().get("userInfo") != null) {        return invocation.invoke();    } else {        ActionSupport action = (ActionSupport) invocation.getAction();        action.addFieldError("authority_interceptor_msg", action.getText("authority_interceptor_msg"));        return ActionSupport.LOGIN;    }}



注:可通过invocation获取该次请求的action实例,通过强制转换得到ActionSupport接口,因为要输出未登陆的消息,就需要借助ActionSupport接口的addFieldError和getText方法,getText是国际化输出用的,把上面的anthority_interceptor_msg在msg文件中配置可以输出不同语言的消息

3.拦截器类实现后运行一下程序,发现没有生效,原因是拦截器栈,因为每个struts.xml都有一个默认的拦截器栈,如果不自动定义的话,struts框架会自动设置<default-interceptor-ref name="defaultStack"></default-interceptor-ref>,在导入的包struts2-core-2.3.4中,有一个struts-default.xml文件,我们在写struts.xml时,都会继承这个包的:<package name="mystruts2" extends="struts-default">,在struts-default.xml文件中,有一个节点:<package name="struts-default" abstract="true">,该节点中定义了struts框架的很多拦截器,还有很多拦截器栈,在最后面,有这样一行:<default-interceptor-ref name="defaultStack"/>,意思是默认的拦截器栈就是上面定义的一个拦截器栈,所以如果我们不修改这个拦截器栈,那么用的还是struts框架默认的拦截器栈
修改如下:
<package name="struts2" extends="struts-default">    <interceptors>        <interceptor name="authority" class="com.struts2.interceptor.AuthorityInterceptor">            <param name="excludeMethods">login,register</param>        </interceptor>        <interceptor-stack name="mystack">            <interceptor-ref name="authority"></interceptor-ref>            <interceptor-ref name="defaultStack"></interceptor-ref>        </interceptor-stack>    </interceptors>    <default-interceptor-ref name="mystack"></default-interceptor-ref></package>

注:添加时注意一下顺序,不然会报错


注:另一种方法也可以实现拦截作用,那就是implements Interceptor接口,但是我实现了该接口后,<param name="excludeMethods">login,register</param>失效,就算在登陆页面也被拦截了。要想用这种方法实现拦截器,只能用逻辑来实现,即在代码中检查请求是不是login方法,如果是就invocation.invoke(),这样实现感觉与过滤器filter差不多,也是在代码中进行逻辑分析,既然用了struts框架,那就最好也把struts的思想也要了解,struts框架是尽量避免再用到request和response这两个变量,所有与这两个变量有关的东西都隐藏了(基本上都是以Object的形式隐藏在Map中),所以最好不要用这种方法实现,我写了一个用来测试的,代码如下:

public class AuthorityInterceptor implements Interceptor {    @Override    public void destroy() {    }    @Override    public void init() {    }    @Override    public String intercept(ActionInvocation invocation) throws Exception {        if (invocation.getInvocationContext().getSession().get("userInfo") != null) {            return invocation.invoke();        } else {            String req = invocation.getInvocationContext().getName();            if (req.equals("login")) {                return invocation.invoke();            }            ActionSupport action = (ActionSupport) invocation.getAction();            action.addFieldError("authority_interceptor_msg", action.getText("authority_interceptor_msg"));            return ActionSupport.LOGIN;        }    }    }


原创粉丝点击