SpringAop获取自定义注解
@Aspect
@Component
@RequiredArgsConstructor
public class AuthCheck {
private final RedisUtils redisUtils;
@Pointcut(value = "@annotation(com.ct.common.core.auth.annotation.RequiresPermissions)")
public void pointCut() {
}
@Before("pointCut()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
Object target = joinPoint.getTarget();
RequiresPermissions permissions = joinPoint
.getSignature()
.getDeclaringType()
.getMethod(methodName)
.getAnnotation(RequiresPermissions.class);
String[] strings = permissions.havesRole();
String permiss = permissions.havesPermiss();
UserHeardBean userHeardBean = HttpHeaderUtils.getUserHeardBean(SpringMvcUtils.getRequest());
String tokenKey = userHeardBean.getTokenKey();
SysUserLogin sysUserLogin = redisUtils.get(CacheConstants.LOGIN_TOKEN_KEY + tokenKey, SysUserLogin.class);
Set<String> premiss = sysUserLogin.getPremiss();
Set<String> roles = sysUserLogin.getRoles();
if (Objects.nonNull(strings) && strings.length > 0) {
HashSet<String> strings1 = new HashSet<>(Arrays.asList(strings));
if (!roles.containsAll(strings1)) {
throw new PermissException("您没有操作的权限", ResponseCodeConstants.NO_PERMISS);
}
}
if (StrUtil.isNotBlank(permiss)) {
if (!premiss.contains(permiss)) {
throw new PermissException("您没有操作的权限", ResponseCodeConstants.NO_PERMISS);
}
}
}
}