参考外链:http://www.ibm.com/developerworks/cn/java/j-lo-springaopfilter/
1.首先,spring的AOP作用范围很广,可以使用Aspectj的execution表达式自定以切面的位置。
比如下面的配置service方法执行日志:
1 <!-- 系统日志 --> 2 <bean id="logUtils" class="com.tabchanj.crm.utils.SystemLogUtils"> 3 <property name="logService" ref="systemLogService"></property> 4 </bean> 5 6 <aop:config> 7 <aop:pointcut expression="execution(* com.tabchanj.crm.service..*.*(..))" id="logPointcut"/> 8 <aop:aspect ref="logUtils"> 9 <aop:after method="writeLog" pointcut-ref="logPointcut" /> 10 </aop:aspect> 11 </aop:config>
2.而struts2的拦截器,继承于AbstractInterceptor,并覆盖其中的intercept方法,它只能作用于action,对所有的action进行拦截。
1 package com.tabchanj.pss.web.interceptor; 2 3 import java.util.Arrays; 4 import java.util.List; 5 6 import com.opensymphony.xwork2.Action; 7 import com.opensymphony.xwork2.ActionContext; 8 import com.opensymphony.xwork2.ActionInvocation; 9 import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 10 import com.tabchanj.pss.domain.Employee; 11 import com.tabchanj.pss.service.IEmployeeService; 12 import com.tabchanj.pss.web.action.BaseAction; 13 14 public class LoginInterceptor extends AbstractInterceptor { 15 16 private static final long serialVersionUID = 3673324815465858383L; 17 private List<String> actionList; 18 private IEmployeeService employeeService; 19 20 public void setEmployeeService(IEmployeeService employeeService) { 21 this.employeeService = employeeService; 22 } 23 24 public void setActionList(String actionList) { 25 26 this.actionList = Arrays.asList(actionList.split(",")); 27 } 28 29 @Override 30 public String intercept(ActionInvocation invocation) throws Exception { 31 Object action = invocation.getAction(); 32 if (actionList.contains(action.getClass().getSimpleName())) { 33 return invocation.invoke(); 34 } 35 Employee employee = (Employee) ActionContext.getContext().getSession().get(BaseAction.LOGIN_USER); 36 if (employee == null) { 37 return Action.LOGIN; 38 } 39 // 查询出该action的全类名 40 String className = action.getClass().getName(); 41 // 查询出当前所要访问的方法 42 String methodName = invocation.getProxy().getMethod(); 43 // 将当前要访问的全类名和要访问的方法拼在一起 44 String classMethodName = className + "." + methodName; 45 // 拼出该action下的所有方法 46 String allclassMethodName = className + ".ALL"; 47 // 查询出所有的资源method名称 48 List<String> allResourceMethod = employeeService.getAllResourcesMethod(); 49 if (allResourceMethod.contains(classMethodName) || allResourceMethod.contains(allclassMethodName)) { 50 // 查询出该当前用户所用的资源列表 51 List<String> loginUserMethods = employeeService.findAllResourcesByLogin(employee.getId()); 52 System.out.println("用户的资源2" + loginUserMethods); 53 if (loginUserMethods.contains(classMethodName) || loginUserMethods.contains(allclassMethodName)) { 54 return invocation.invoke(); 55 } else { 56 return "auth"; 57 } 58 } 59 return invocation.invoke(); 60 } 61 62 }
3.而springMVC的拦截器,只拦截Controller,其实现于HandlerInterceptor接口,并根据需要覆盖其中的3个分别在controller前后执行的方法,
1 package com.tabchanj.crm.web.interceptor; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.method.HandlerMethod; 7 import org.springframework.web.servlet.HandlerInterceptor; 8 import org.springframework.web.servlet.ModelAndView; 9 10 import com.tabchanj.crm.domain.Employee; 11 import com.tabchanj.crm.utils.UserContext; 12 13 public class AuthInterceptor implements HandlerInterceptor { 14 15 public AuthInterceptor() { 16 System.out.println("AuthInterceptor初始化。。。。"); 17 } 18 19 public static final String LOGIN_PATH = "/checkLogin"; 20 21 /** 22 * 1.在调用控制器方法前,拦截 23 * 24 * 返回值为false,代表拦截 返回值为true,代表放行 25 */ 26 @Override 27 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 28 throws Exception { 29 // System.out.println("AuthInterceptor....preHandle"); 30 /** 31 * 1、登陆验证 32 */ 33 // 获取session用户 34 Employee user = UserContext.getUser(); 35 // 检查用户是否存在 36 if (user == null && !LOGIN_PATH.equals(request.getRequestURI())) { 37 response.sendRedirect("/login.jsp"); 38 return false; 39 } 40 41 /** 42 * 2、URL权限验证 43 */ 44 // 1、获取用户请求的地址 45 // 把handler转变成HandlerMethod对象 46 if (handler instanceof HandlerMethod) { 47 HandlerMethod hm = (HandlerMethod) handler; 48 // 获取控制器名称 49 String controllerName = hm.getBean().getClass().getName(); 50 // 获取方法名; 51 String methodName = hm.getMethod().getName(); 52 // 拼装resource名称 53 String resourceName = controllerName + ":" + methodName; 54 // 4.验证用户上,是否有请求资源对应的权限 55 if (UserContext.checkUserResPermission(resourceName)) { 56 return true;// 放行 57 } else { 58 response.sendRedirect("/data/noPermission.json");// 拦截,返回提示界面 59 return false; 60 61 } 62 } 63 64 return true;// 放行 65 66 } 67 68 /** 69 * 2.在调用控制器方法后,拦截(在生成视图之前) 70 */ 71 @Override 72 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 73 ModelAndView modelAndView) throws Exception { 74 // System.out.println("AuthInterceptor..postHandle....."); 75 76 } 77 78 /** 79 * 3.在视图生成之后(后台所有所有逻辑都完成后) 80 */ 81 @Override 82 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 83 throws Exception { 84 // System.out.println("AuthInterceptor...afterCompletion....."); 85 86 } 87 88 }