AppRoleAspect.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.poyee.common.service.aop;
  2. import com.poyee.common.service.annotation.RequireRoles;
  3. import com.poyee.common.service.common.user.UserInfo;
  4. import com.poyee.common.service.common.user.UserUtils;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.Signature;
  8. import org.aspectj.lang.annotation.Around;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Pointcut;
  11. import org.aspectj.lang.reflect.MethodSignature;
  12. import org.springframework.stereotype.Component;
  13. import java.lang.reflect.Method;
  14. import java.util.*;
  15. @Slf4j
  16. @Aspect
  17. @Component
  18. public class AppRoleAspect {
  19. // 配置织入点
  20. @Pointcut("@annotation(cn.hobbystocks.auc.annotation.RequireRoles)")
  21. public void rolePointCut() {
  22. }
  23. // @RequireRoles({"admin"})
  24. @Around("cn.hobbystocks.auc.aop.AppRoleAspect.rolePointCut()")
  25. public Object roleBefore(ProceedingJoinPoint pjp) throws Throwable {
  26. UserInfo user = UserUtils.getSimpleUserInfo();
  27. Signature signature = pjp.getSignature();
  28. MethodSignature methodSignature = (MethodSignature) signature;
  29. Method targetMethod = methodSignature.getMethod();
  30. RequireRoles annotation = targetMethod.getAnnotation(RequireRoles.class);
  31. String[] roles = annotation.value();
  32. List<String> requirePermissions = Arrays.asList(roles);
  33. List<String> userPermissions = new ArrayList<>();
  34. String role = user.getRoleCode() != null ? user.getRoleCode() : "";
  35. userPermissions.add(role);
  36. List<String> permissionsList = user.getPermissionsList();
  37. // if (!CollectionUtils.isEmpty(permissionsList)) {
  38. // userPermissions.addAll(permissionsList);
  39. // }
  40. // Set<String> commonRole = getCommonElements(requirePermissions,
  41. // userPermissions);
  42. // if (CollectionUtils.isEmpty(commonRole)) {
  43. // log.info("权限不足,用户信息:{}",user);
  44. // return AjaxResult.error("权限不足!");
  45. // }
  46. return pjp.proceed();
  47. }
  48. private Set<String> getCommonElements(List<String> list1, List<String> list2) {
  49. // if (CollectionUtils.isEmpty(list1) || CollectionUtils.isEmpty(list2)) {
  50. // return null;
  51. // }
  52. Set<String> set1 = new HashSet<>(list1);
  53. Set<String> set2 = new HashSet<>(list2);
  54. Set<String> commonElements = new HashSet<>(set1);
  55. commonElements.retainAll(set2);
  56. return commonElements;
  57. }
  58. }