博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
权限管理系统 AOP切面编程控制权限之切面类
阅读量:2346 次
发布时间:2019-05-10

本文共 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(转载),可参考此文

你可能感兴趣的文章
Storm部署文档
查看>>
申请Let's Encrypt的证书
查看>>
Keyless SSL: The Nitty Gritty Technical Details
查看>>
AliSQL开源Sequence Engine
查看>>
如何看Cortex-M系列处理器差异与共性?技术老司机Joseph带你飞
查看>>
贾扬清撰文详解Caffe2:从强大的新能力到入门上手教程
查看>>
c++协程1 (boost::coroutine)
查看>>
c++协程2 (boost::coroutine)
查看>>
c++协程3 (boost::coroutine)
查看>>
c++协程4 (boost::coroutine)
查看>>
谷歌发布第二代TPU,并提供了免费试用方案
查看>>
Android O 迁移应用官方指南
查看>>
如何通过OpenFace实现人脸识别框架
查看>>
人脸检测与识别年度进展概述
查看>>
央行数字货币研究所悄然挂牌 工作人员:已有一段时间
查看>>
Angle和XBGoost以及Spark的性能对比
查看>>
IOS CoreImage实现人脸识别
查看>>
Tensorflow的高级封装
查看>>
视觉目标检测和识别之过去,现在及可能
查看>>
现代c++之列表初始化/统一初始化
查看>>