zoukankan      html  css  js  c++  java
  • struts2 权限拦截器 拦截没有登陆的请求

    假设有这样的登陆:

    ActionContext.getContext().getSession().put("UserMsg", userMsg);

    则可以这样判断是否登陆:

    import com.ahgw.common.sessionBean.UserMsg;
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    /**
     * 负责后台权限访问(必须登陆才可以进入后台)
     * User: HYY
     * Date: 13-5-27
     * Time: 下午8:27
     * To change this template use File | Settings | File Templates.
     */
    public class AdminInterceptor extends AbstractInterceptor {
    
        @Override
        public String intercept(ActionInvocation actionInvocation) throws Exception {
            ActionContext context = actionInvocation.getInvocationContext();
            UserMsg userMsg = (UserMsg) context.getSession().get("UserMsg");
            if (userMsg == null) {
                return Action.LOGIN;
            }
            return actionInvocation.invoke();
        }
    }

    其相应的strut配置如下:

    <struts>
        <package name="admin-newsManage" extends="struts-default,json-default" namespace="/newsManage">
            <interceptors>
                <interceptor name="privilege" class="com.ahgw.admin.interceptor.AdminInterceptor"/>
                <interceptor-stack name="privilegeStack">
                    <interceptor-ref name="defaultStack"/>
                    <interceptor-ref name="privilege"/>
                </interceptor-stack>
            </interceptors>
    
            <default-interceptor-ref name="privilegeStack"/>
    
            <global-results>
                <result name="login" type="redirect">/admin.do</result>
            </global-results>
    
        </package>
    </struts>

    这样,该package下的所有action中的execute方法进行之前,都会经过interceptor拦截器的判断,即进行权限判断,如果已经登陆,则会转入action中,否则返回登陆视图。

    其中包含struts2拦截器堆栈的知识,这里不进行展开了。

  • 相关阅读:
    JDom写入XML例子
    hdu 2549
    hdu 1328
    hdu 1334
    hdu 2547
    hdu 2374
    hdu 2550
    hdu 1335
    hdu 2548
    hdu 1722
  • 原文地址:https://www.cnblogs.com/wuyou/p/3313207.html
Copyright © 2011-2022 走看看