| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package com.poyee.common.service.aop;
- import com.poyee.common.service.annotation.RequireRoles;
- import com.poyee.common.service.common.user.UserInfo;
- import com.poyee.common.service.common.user.UserUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.Signature;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
- import org.aspectj.lang.reflect.MethodSignature;
- import org.springframework.stereotype.Component;
- import java.lang.reflect.Method;
- import java.util.*;
- @Slf4j
- @Aspect
- @Component
- public class AppRoleAspect {
- // 配置织入点
- @Pointcut("@annotation(cn.hobbystocks.auc.annotation.RequireRoles)")
- public void rolePointCut() {
- }
- // @RequireRoles({"admin"})
- @Around("cn.hobbystocks.auc.aop.AppRoleAspect.rolePointCut()")
- public Object roleBefore(ProceedingJoinPoint pjp) throws Throwable {
- UserInfo user = UserUtils.getSimpleUserInfo();
- Signature signature = pjp.getSignature();
- MethodSignature methodSignature = (MethodSignature) signature;
- Method targetMethod = methodSignature.getMethod();
- RequireRoles annotation = targetMethod.getAnnotation(RequireRoles.class);
- String[] roles = annotation.value();
- List<String> requirePermissions = Arrays.asList(roles);
- List<String> userPermissions = new ArrayList<>();
- String role = user.getRoleCode() != null ? user.getRoleCode() : "";
- userPermissions.add(role);
- List<String> permissionsList = user.getPermissionsList();
- // if (!CollectionUtils.isEmpty(permissionsList)) {
- // userPermissions.addAll(permissionsList);
- // }
- // Set<String> commonRole = getCommonElements(requirePermissions,
- // userPermissions);
- // if (CollectionUtils.isEmpty(commonRole)) {
- // log.info("权限不足,用户信息:{}",user);
- // return AjaxResult.error("权限不足!");
- // }
- return pjp.proceed();
- }
- private Set<String> getCommonElements(List<String> list1, List<String> list2) {
- // if (CollectionUtils.isEmpty(list1) || CollectionUtils.isEmpty(list2)) {
- // return null;
- // }
- Set<String> set1 = new HashSet<>(list1);
- Set<String> set2 = new HashSet<>(list2);
- Set<String> commonElements = new HashSet<>(set1);
- commonElements.retainAll(set2);
- return commonElements;
- }
- }
|