zoukankan      html  css  js  c++  java
  • struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解

    通过对struts2的学习,对于interceptor中的excludeMethods与includeMethods的理解:

    针对MethodFilterInterceptor:

    excludeMethods表示排除指定的方法,即不对标记为excludeMethods的方法进行拦截,

    includeMethods表示包含指定的方法,即对标记为includeMethods的方法进行拦截,

    在struts.xml中关于excludeMethods和includeMethods有两种实现方式,一种相当于全局,另一种相当于局部,即

    <interceptors>
    <interceptor name="method" class="com.yxl.interceptor.MethodInterceptor">
    <param name="includeMethods">method1,method2</param>
    </interceptor>
    </interceptors>

    为全局。

    <interceptor-ref name="method">
    <param name="excludeMethods">method1,method2</param>
    </interceptor-ref> 

    为局部,

    若全局中的param定义为excludeMethods同样局部中的param也定义为excludeMethods,则局部中的param生效,全局中的param无效,即被局部中的param覆盖,同样,若全局中的param定义为includeMethods同样局部中的param也定义为includeMethods,则局部中的param生效,全局中的param无效,即被局部中的param覆盖。

    当全局中的param与局部中的param不相同的时,即当全局中param为excludeMethods而局部中的param为includeMethods或全局中的param为includeMethods而局部中param为excludeMethods,则标志为includeMethods生效。即若是全局中的param定义为includeMethods,则全局屏蔽局部,以全局为准,反之,若局部的param定义为includeMethods,则以局部为准。

    如果即没有指定includeMethods也没有指定excludeMethods的方法默认的时includeMethos方法

    如果仅仅指定了includeMethods的方法,没有包含在includeMethods里的方法就不会被拦截。

     

     要实现自定义拦截器,需要继承MethodFilterInterceptor类。MethodFilterInterceptor类是AbstractInterceptor的子类,其源代码如下:

    package com.opensymphony.xwork2.interceptor;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.util.TextParseUtil;
    import com.opensymphony.xwork2.util.logging.Logger;
    import com.opensymphony.xwork2.util.logging.LoggerFactory;
    
    import java.util.Collections;
    import java.util.Set;
    
    public abstract class MethodFilterInterceptor extends AbstractInterceptor {
        protected transient Logger log = LoggerFactory.getLogger(getClass());
        
        protected Set<String> excludeMethods = Collections.emptySet();
        protected Set<String> includeMethods = Collections.emptySet();
    
        public void setExcludeMethods(String excludeMethods) {
            this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
        }
        
        public Set<String> getExcludeMethodsSet() {
            return excludeMethods;
        }
    
        public void setIncludeMethods(String includeMethods) {
            this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);
        }
        
        public Set<String> getIncludeMethodsSet() {
            return includeMethods;
        }
    
        @Override
        public String intercept(ActionInvocation invocation) throws Exception {
            if (applyInterceptor(invocation)) {
                return doIntercept(invocation);
            } 
            return invocation.invoke();
        }
    
        protected boolean applyInterceptor(ActionInvocation invocation) {
            String method = invocation.getProxy().getMethod();
            // ValidationInterceptor
            boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);
            if (log.isDebugEnabled()) {
                if (!applyMethod) {
                    log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.");
                }
            }
            return applyMethod;
        }
        
        /**
         * Subclasses must override to implement the interceptor logic.
         * 
         * @param invocation the action invocation
         * @return the result of invocation
         * @throws Exception
         */
        protected abstract String doIntercept(ActionInvocation invocation) throws Exception;
        
    }


    只需要实现该类中的如下方法即可

    protected abstract String doIntercept(ActionInvocation invocation) throws Exception 
    样例代码:
    package cua.survey.interceptor;
    
    import java.util.Map;
    
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    
    public class LoginInterceptor extends MethodFilterInterceptor{
    
        private static final long serialVersionUID = 1L;
    
        protected String doIntercept(ActionInvocation action) throws Exception {
            Map<String, Object> session = ActionContext.getContext().getSession();
            String user = (String)session.get("user");
            if(user != null && !"".equals(user)){
    //通过
                return action.invoke();
            }else{
    //不通过,报错
                session.put("error", "your user or pwd is error, please login again...");
                return Action.LOGIN;
            }
    
        }
    
    }

      

    实现之后拦截器属性excludeMethods、includeMethods就可以起到作用了

    相关:

    拦截器的基本使用方法

  • 相关阅读:
    html+css基础
    Jquery初级学习-实例3--视频网站视频展示效果
    Jquery初级学习-实例3--网站超链接和图片提示效果
    kali优化配置(3)--工具箱
    Raven2
    kali优化配置(2)
    kali优化配置(1)
    实验(1)
    sqli(7)
    CtfStudying之SSH私钥泄露
  • 原文地址:https://www.cnblogs.com/langtianya/p/3012205.html
Copyright © 2011-2022 走看看