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 requirePermissions = Arrays.asList(roles); List userPermissions = new ArrayList<>(); String role = user.getRoleCode() != null ? user.getRoleCode() : ""; userPermissions.add(role); List permissionsList = user.getPermissionsList(); // if (!CollectionUtils.isEmpty(permissionsList)) { // userPermissions.addAll(permissionsList); // } // Set commonRole = getCommonElements(requirePermissions, // userPermissions); // if (CollectionUtils.isEmpty(commonRole)) { // log.info("权限不足,用户信息:{}",user); // return AjaxResult.error("权限不足!"); // } return pjp.proceed(); } private Set getCommonElements(List list1, List list2) { // if (CollectionUtils.isEmpty(list1) || CollectionUtils.isEmpty(list2)) { // return null; // } Set set1 = new HashSet<>(list1); Set set2 = new HashSet<>(list2); Set commonElements = new HashSet<>(set1); commonElements.retainAll(set2); return commonElements; } }