struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解

参考:http://www.cnblogs.com/langtianya/archive/2013/04/10/3012205.html

通过对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,则全局屏蔽局部,以全局为准,反之,以局部为准。

 

 

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

  1.     protected transient Logger log = LoggerFactory.getLogger(getClass());  
  2.       
  3.     protected Set<String> excludeMethods = Collections.emptySet();  
  4.     protected Set<String> includeMethods = Collections.emptySet();  
  5.   
  6.     public void setExcludeMethods(String excludeMethods) {  
  7.         this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);  
  8.     }  
  9.       
  10.     public Set<String> getExcludeMethodsSet() {  
  11.         return excludeMethods;  
  12.     }  
  13.   
  14.     public void setIncludeMethods(String includeMethods) {  
  15.         this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);  
  16.     }  
  17.       
  18.     public Set<String> getIncludeMethodsSet() {  
  19.         return includeMethods;  
  20.     }  
  21.   
  22.     @Override  
  23.     public String intercept(ActionInvocation invocation) throws Exception {  
  24.         if (applyInterceptor(invocation)) {  
  25.             return doIntercept(invocation);  
  26.         }   
  27.         return invocation.invoke();  
  28.     }  
  29.   
  30.     protected boolean applyInterceptor(ActionInvocation invocation) {  
  31.         String method = invocation.getProxy().getMethod();  
  32.         // ValidationInterceptor   
  33.         boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);  
  34.         if (log.isDebugEnabled()) {  
  35.             if (!applyMethod) {  
  36.                 log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.");  
  37.             }  
  38.         }  
  39.         return applyMethod;  
  40.     }  
  41.       
  42.     /** 
  43.      * Subclasses must override to implement the interceptor logic. 
  44.      *  
  45.      * @param invocation the action invocation 
  46.      * @return the result of invocation 
  47.      * @throws Exception 
  48.      */  
  49.     protected abstract String doIntercept(ActionInvocation invocation) throws Exception;  
  50.       
  51. }  
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

即可。

 

 

样例代码:

 

  1.   
  2. import java.util.Map;  
  3.   
  4. import com.opensymphony.xwork2.Action;  
  5. import com.opensymphony.xwork2.ActionContext;  
  6. import com.opensymphony.xwork2.ActionInvocation;  
  7. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  8. import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;  
  9.   
  10. public class LoginInterceptor extends MethodFilterInterceptor{  
  11.   
  12.     private static final long serialVersionUID = 1L;  
  13.   
  14.     protected String doIntercept(ActionInvocation action) throws Exception {  
  15.         Map<String, Object> session = ActionContext.getContext().getSession();  
  16.         String user = (String)session.get("user");  
  17.         if(user != null && !"".equals(user)){  
  18.             return action.invoke();  
  19.         }else{  
  20.             session.put("error""your user or pwd is error, please login again...");  
  21.             return Action.LOGIN;  
  22.         }  
  23.   
  24.     }  
  25.   
  26. }  
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就可以起到作用了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts2 ,可以使用拦截来实现权限控制。具体来说,可以编写一个自定义的拦截,然后在 struts.xml 配置文件将其配置为需要进行权限控制的 Action 的拦截。以下是一个简单的权限拦截示例: ```java public class AuthInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { // 获取当前用户 User user = (User) ActionContext.getContext().getSession().get("user"); // 判断用户是否拥有权限 if (user != null && user.hasPermission(invocation.getInvocationContext().getName())) { // 如果有权限,则继续执行 Action return invocation.invoke(); } else { // 如果没有权限,则跳转到错误页面 return "error"; } } } ``` 在上述代码,我们首先获取了当前用户,然后判断用户是否拥有执行当前 Action 的权限。如果有权限,则继续执行 Action;否则,跳转到错误页面。 接下来,在 struts.xml 配置文件,我们可以将该拦截配置为需要进行权限控制的 Action 的拦截,如下所示: ```xml <action name="myAction" class="com.example.MyAction"> <interceptor-ref name="authInterceptor"/> <result name="success">/myAction.jsp</result> <result name="error">/error.jsp</result> </action> <interceptor-stack name="authStack"> <interceptor-ref name="authInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> <interceptors> <interceptor name="authInterceptor" class="com.example.AuthInterceptor"/> </interceptors> ``` 在上述代码,我们首先定义了一个名为 authStack 的拦截栈,该拦截栈包含了 authInterceptor 和 defaultStack 两个拦截。然后,我们将 myAction Action 配置为使用 authStack 拦截栈。最后,我们定义了一个名为 authInterceptor拦截,并将其添加到了 interceptors 。 这样一来,当用户访问 myAction Action 时,就会先执行 authInterceptor 拦截,进行权限控制,然后再执行 defaultStack 拦截的其它拦截。如果 authInterceptor 拦截返回了 error,则会跳转到 error.jsp 页面;否则,会跳转到 myAction.jsp 页面。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值