今天没事干就写了个这么个玩意、目的是用在权限管理上。写得时候、头都大了,缩减了一次代码。请大家给我找找毛病,争取完善
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;


public class LimitInterceptor implements Interceptor{
  public void destroy() {
  }
  public void init() {
  }

  public String intercept(ActionInvocation actionInvocation) throws Exception {
    ActionContext context=actionInvocation.getInvocationContext();
    HttpServletRequest request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
    HttpSession session=request.getSession();
    if(request.getHeader("Referer")!=null&&session.getAttribute("userinfo")!=null){
//        获得用户
        EntityManager em=new EntityManager(session.getAttribute("userinfo").toString());
//        数据库所有权限
        HashMap<String,String> limitsMap=LimitsUtils.getInstance().getAllLimis();
//        获得该用户权限    
        String[]limit=limitsMap.get(em.lv).toString().split("/");//delete/view
          if(limit==null){
            returnLastPage();
            return null;
          }else{
            String methodName;
            for(int stmp=0;stmp<limit.length;stmp++){
              methodName=actionInvocation.getProxy().getMethod();
              Pattern p = Pattern.compile("^"+limit[stmp]+"([a-zA-Z0-9_//$]+?)$");    
              //匹配用户权限
              Matcher m = p.matcher(methodName.toLowerCase());    
                         if(m.matches()){
                            return actionInvocation.invoke();
                         }else{
                            returnLastPage();
                            return null;
                         }
            }
            return null;
          }
    }else{
      return "index";
    }
  }
    
  public void returnLastPage()
  {
    try {
      ServletActionContext.getResponse().setContentType("text/html");
      ServletActionContext.getResponse().setCharacterEncoding("utf-8");
      ServletActionContext.getResponse().getWriter().write("<script language='javascript'>alert('您没有该权限执行这项任务');history.go(-1);</script>");
      ServletActionContext.getResponse().getWriter().flush();
      ServletActionContext.getResponse().getWriter().close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
    
  /**
    * 醉翁之意不在酒
    * 此处只是暂用之
    * @author Administrator
    *
    */

  class EntityManager{
    private String name;
    private List<String> limits;
    private String lv;
    public EntityManager(String name) {
      super();
      this.name = name;
    }
    public EntityManager() {
      super();
      // TODO Auto-generated constructor stub
    }
    public String getLv() {
      if(lv==null)lv="User";
      return lv;
    }
    public void setLv(String lv) {
      this.lv = lv;
    }
  }
    
  static class LimitsUtils{
    private LimitsUtils(){}
    private static LimitsUtils limitsutils;
    public static LimitsUtils getInstance(){
      if(limitsutils==null){
      synchronized (LimitsUtils.class) {
        if(limitsutils==null){
          limitsutils=new LimitsUtils();
        }
      }
      }
      return limitsutils;
    }
    public HashMap<String,String> getAllLimis() {
      HashMap<String,String> dataAllLimits=new HashMap<String, String>();
      dataAllLimits.put("Admin","add/delete/update/view");
      dataAllLimits.put("Proxy","add/view");
      dataAllLimits.put("User","view");
      return dataAllLimits;
    }
  }
}