对于B/S应用的权限拦截主要有粗密度和细密度两种:
1 import java.lang.reflect.Method; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 import org.apache.struts2.ServletActionContext; 6 import org.aspectj.lang.annotation.Aspect; 7 8 import com.opensymphony.xwork2.ActionInvocation; 9 import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 10 import com.yixi.shopping.model.privilege.Employee; 11 import com.yixi.shopping.model.privilege.PrivilegeGroup; 12 import com.yixi.shopping.model.privilege.SystemPrivilege; 13 import com.yixi.shopping.model.privilege.SystemPrivilegePK; 14 import com.yixi.shopping.util.SiteUrl; 15 import com.yixi.shopping.util.WebUtil; 16 import com.yixi.shopping.web.action.privilege.Permission; 17 18 public class PermissionInterceptor extends AbstractInterceptor { 19 20 private static final long serialVersionUID = -446959629705298377L; 21 22 /** 23 * 1、通过动态代理类和反射机制获取调用的方法 24 * 2、获取方法的Permission注解 25 * 3、获取Permission中包含的权限信息(构造SystemPrivilege权限类) 26 * 4、和用户的权限信息进行对比(PrivilegeGroup中的SystemPrivilege)判断用户的权限 27 */ 28 @Override 29 public String intercept(ActionInvocation invocation) throws Exception { 30 31 HttpServletRequest request = ServletActionContext.getRequest(); 32 if(WebUtil.getRequestURI(request).startsWith("/control/")){ 33 String methodName = invocation.getProxy().getMethod(); 34 Method method = invocation.getAction().getClass().getMethod(methodName); 35 if(method!=null && method.isAnnotationPresent(Permission.class)){ 36 System.out.println("Call the Action Method:" + method.toString()); 37 //获取当期执行的Action上的注解 38 Permission permission = method.getAnnotation(Permission.class); 39 System.out.println("当前需要的权限为:" + permission.moudle()); 40 //获取当期执行的Action方法需要的权限 41 SystemPrivilege methodPrivilege = new SystemPrivilege(new SystemPrivilegePK(permission.moudle(),permission.methid())); 42 //WebUtil.getEmployee(request) 方法是从session中获取保存的employee信息,包括权限集合 43 Employee employee = WebUtil.getEmployee(request); 44 //循环判断用户是否具有该权限,如果有则继续执行,否则返回提示视图 45 for(PrivilegeGroup group : employee.getGroups()){ 46 if(group.getPrivileges().contains(methodPrivilege)){ 47 return invocation.invoke(); 48 } 49 } 50 System.out.println("权限不足"); 51 request.setAttribute("message", "你没有执行该操作的权限"); 52 request.setAttribute("urladdress", SiteUrl.readUrl("control.center.right")); 53 return "message"; 54 } 55 System.out.println("未设置权限"); 56 return invocation.invoke(); 57 } 58 System.out.println("所有权限"); 59 return invocation.invoke(); 60 } 61 62 }
1 import java.util.Set; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.jsp.JspException; 5 import javax.servlet.jsp.tagext.TagSupport; 6 7 import com.yixi.shopping.model.privilege.Employee; 8 import com.yixi.shopping.model.privilege.PrivilegeGroup; 9 import com.yixi.shopping.model.privilege.SystemPrivilege; 10 import com.yixi.shopping.model.privilege.SystemPrivilegePK; 11 import com.yixi.shopping.util.WebUtil; 12 13 /** 14 * 15 * 定义权限标签 16 * @author 罗宏 17 * 18 */ 19 public class PermissionTaglib extends TagSupport { 20 private static final long serialVersionUID = -1603078353787859001L; 21 22 private String moudle; 23 private String methid; 24 public String getMoudle() { 25 return moudle; 26 } 27 public void setMoudle(String moudle) { 28 this.moudle = moudle; 29 } 30 public String getMethid() { 31 return methid; 32 } 33 public void setMethid(String methid) { 34 this.methid = methid; 35 } 36 37 @Override 38 public int doStartTag() throws JspException { 39 boolean result= false; 40 Employee employee = WebUtil.getEmployee((HttpServletRequest) pageContext.getRequest()); 41 SystemPrivilege systemPrivilege = new SystemPrivilege(new SystemPrivilegePK(moudle, methid)); 42 Set<PrivilegeGroup> set = employee.getGroups(); 43 for (PrivilegeGroup privilegeGroup : set) { 44 if(privilegeGroup.getPrivileges().contains(systemPrivilege)){ 45 result = true; 46 break; 47 } 48 } 49 return result ? EVAL_BODY_INCLUDE : SKIP_BODY; 50 } 51 }