本文共 5574 字,大约阅读时间需要 18 分钟。
@Component @Aspect public class PermissionAspect { @Resource private UserService userservice; @Resource private RoleService roleservice; @Resource private User_RoleService user_roleservice; ThreadUtils Tutils = ThreadUtils.getInstance(); public User getUser() { User user = Tutils.getUserId(); return user; } @Pointcut("execution(* com.funo.oa.web.*.*(..))") private void anyMethod() { } // 声明一个切入点,anyMethod为切入点名称 /** * 前置通知:目标方法执行之前执行以下方法体的内容 * * @param jp */ // @Before("execution(* com.funo.oa.serviceImpl.*.*(..))") @Before("anyMethod()") public void beforeMethod(JoinPoint jp) { /* * User user = (User) request.getAttribute("currentUser"); Set<User_Role> * userroles = user.getUserroles(); Iterator<User_Role> iterator = * userroles.iterator(); while(iterator.hasNext()) { User_Role user_Role = * iterator.next(); Role role = user_Role.getRole(); Set<Role_Operation> * roleoperas = role.getRoleoperas(); Iterator<Role_Operation> iterator2 = * roleoperas.iterator(); while(iterator2.hasNext()) { Role_Operation * role_Operation = iterator2.next(); Operation operation = * role_Operation.getOperation(); * operationList.add(operation.getPermissionSign()); } } */ // String methodName = jp.getSignature().getName(); // MethodSignature signature = (MethodSignature) jp.getSignature(); // PermissionController annotation = // signature.getMethod().getAnnotation(PermissionController.class); // System.out.println("【前置通知】the method 【" + methodName + "】 begins with " + // Arrays.asList(jp.getArgs())); // if(annotation!=null) { // LogUtil.error(annotation.permissionName()[0]); // } /* * if(annotation!=null) { String[] permissionName = annotation.permissionName(); * for (int i = 0; i < permissionName.length; i++) { for (int j = 0; j < * operationList.size(); j++) { * if(permissionName[i].equals(operationList.get(j))) { signature.getMethod(). } * } } * * System.out.println(annotation.permissionName()); } */ } /** * 返回通知:目标方法正常执行完毕时执行以下代码 * * @param jp * @param result */ @AfterReturning(value = "anyMethod()", returning = "result") public void afterReturningMethod(JoinPoint jp, Object result) { String methodName = jp.getSignature().getName(); System.out.println("【返回通知】the method 【" + methodName + "】 ends with 【" + result + "】"); } /** * 后置通知:目标方法执行之后执行以下方法体的内容,不管是否发生异常。 * * @param jp */ @After("anyMethod()") public void afterMethod(JoinPoint jp) { System.out.println("【后置通知】this is a afterMethod advice..."); } /** * 异常通知:目标方法发生异常的时候执行以下代码 */ @AfterThrowing(value = "anyMethod()", throwing = "e") public void afterThorwingMethod(JoinPoint jp, NullPointerException e) { String methodName = jp.getSignature().getName(); System.out.println("【异常通知】the method 【" + methodName + "】 occurs exception: " + e); } /** * 环绕通知:目标方法执行前后分别执行一些代码,发生异常的时候执行另外一些代码 * * @return */ @Around("anyMethod()") public Object aroundMethod(ProceedingJoinPoint jp) { ArrayList<String> operationList = new ArrayList<>(); String methodName = jp.getSignature().getName(); Object result = null; boolean isExist = false; try { System.out.println( "【环绕通知中的--->前置通知】:the method 【" + methodName + "】 begins with " + Arrays.asList(jp.getArgs())); System.out.println(getUser()); // 执行目标方法 /*******************************************************/ User user = getUser(); if (user != null) { Set<User_Role> userroles = user.getUserroles(); Iterator<User_Role> iterator = userroles.iterator(); while (iterator.hasNext()) { User_Role user_Role = iterator.next(); Role role = user_Role.getRole(); Set<Role_Operation> roleoperas = role.getRoleoperas(); Iterator<Role_Operation> iterator2 = roleoperas.iterator(); while (iterator2.hasNext()) { Role_Operation role_Operation = iterator2.next(); Operation operation = role_Operation.getOperation(); operationList.add(operation.getPermissionSign()); } } } // String methodName2 = jp.getSignature().getName(); MethodSignature signature = (MethodSignature) jp.getSignature(); PermissionController annotation = signature.getMethod().getAnnotation(PermissionController.class); System.out.println("【前置通知】the method 【" + methodName + "】 begins with " + Arrays.asList(jp.getArgs())); HttpServletRequest request = null; HttpServletResponse response = null; Object[] args = jp.getArgs(); for (int i = 0; i < args.length; i++) { if (args[i] instanceof HttpServletRequest) { request = (HttpServletRequest) args[i]; } if (args[i] instanceof HttpServletResponse) { response = (HttpServletResponse) args[i]; } } if (annotation != null) { LogUtil.error(annotation.permissionName()[0]); System.out.println(operationList.isEmpty()); } if (annotation != null) { String[] permissionName = annotation.permissionName(); for (int i = 0; i < permissionName.length; i++) { for (int j = 0; j < operationList.size(); j++) { if (permissionName[i].equals(operationList.get(j))) { // result = jp.proceed(); isExist=true; } } } if(isExist) { result = jp.proceed(); }else { response.sendRedirect(request.getContextPath()+"/index.jsp"); result=null; } } else { result = jp.proceed(); } /*****************************************************************/ System.out.println("【环绕通知中的--->返回通知】:the method 【" + methodName + "】 ends with " + result); } catch (Throwable e) { System.out.println("【环绕通知中的--->异常通知】:the method 【" + methodName + "】 occurs exception " + e); } System.out.println("【环绕通知中的--->后置通知】:-----------------end.----------------------"); return result; }}
补充:在切面类中传递参数:
(1)利用ProceedingJoinPoint jp.getArgs()方法即可获得,此参数列表为方法参数中的形参,譬如需要在切面类中使用到HttpServletRequest和HttpServletResponse时,可将其放在方法的参数中,有AOP控制是就可以获取到该参数
(2)http://blog.csdn.net/littleskey/article/details/51842917(转载),可参考此文