1.     首先要在struts的配置文件中进行拦截器的配置
    <package name="sys" extends="struts-default" namespace="/">
       <interceptors>
       <!-- 声明自己的拦截器,起名叫check,对应的class属性为自己编写的拦截器路径 -->
           <interceptor name="check" class="com.scm.actions.sys.AuthorInterceptor" />
           <!-- 定义拦截器栈,这里需要注意:在定制自己拦截器的同时,必须要把struts的默认栈加如里面,如果不加入,相当于把默认的覆盖了,会出现异常! -->
           <interceptor-stack name="myCheck">
              <interceptor-ref name="check" />
              <interceptor-ref name="defaultStack" />
           </interceptor-stack>
       </interceptors>
 
 
       <!-- 定义默认拦截器 -->
       <default-interceptor-ref name="myCheck" />
       
       <!-- 定义全局结果,用于在拦截器中返回登录页面或者错误提示页面 -->
       <global-results>
           <result name="login" type="redirect">/round.html</result>
           <result name="error">/scm_templates/Samples/index.html</result>
       </global-results>
    </package>
 
 
2.     编写拦截器类
package com.scm.actions.sys;
 
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.scm.domain.SysYonghu;
 
/**
 * 拦截器需要继承AbstractInterceptor,或者是实现Interceptor接口(AbstractInterceptor也是实现的Interceptor)
 */
@SuppressWarnings("serial")
public class AuthorInterceptor extends AbstractInterceptor  {
 
    /*不清楚为什么,在拦截器中注入业务层或者数据访问层的对象始终为null,
    但是注入action对象却可以,所以这里我注入action对象,然后在action中
    转去调用业务层的方法..*/
    private YongHuAction yonghuAction;
    //getter and setter...
 
 
    @SuppressWarnings("unchecked")
    public String intercept(ActionInvocation invocation) throws Exception {
       System.out.println("=======检查action=======");
 
       // 获得拦截到的action名称
       String actionName =invocation.getInvocationContext().getName();
       ActionContext ctx = invocation.getInvocationContext();
       Map session = ctx.getSession();
 
       //如果拦截到的action是请求登录的action,则放行.
       if (actionName.equals("logins")) {
           return invocation.invoke();
       } else {
           //session中取得当前用户.
           SysYonghu yonghu = (SysYonghu) session.get("current_user");
           //如果当前用户为空,转到登录页面.
           if(yonghu==null){
              session.put("tip", "请登录...");
              return Action.LOGIN;
           }
//调用action中的方法, 判断有无访问此动作的权限
           boolean result=yonghuAction.isHave(actionName, yonghu);
           if(result){
              return invocation.invoke();
           }else{
              session.put("quanxian_tip", "您没有该资源的权限!");
              return Action.ERROR;
           }
       }
    }
}
 
 3. 在使用的时候,如果有多个struts配置文件,只需要继承”sys”即可.如果其他配置文件中又定义了拦截器.那么这个拦截器将失效.
 
 
 
 
不足之处:拦截器只能拦截访问的action,如果直接访问jsp或其他视图,拦截器无法拦截. 所以应该配合filter使用;其实spring有自己的安全框架.可以把权限分的很细.希望有机会能用到.