1. 程式人生 > >許可權管理系統 AOP切面程式設計控制權限之切面類

許可權管理系統 AOP切面程式設計控制權限之切面類

@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(轉載),可參考此文