1. AOP方式
package com.dj.springboot.study.aspect;
import com.dj.springboot.study.annotation.RolePermission;
import org.aspectj.lang.ProceedingJoinPoint;
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.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
/**
* @Author: ldj
* @Date: 2024/02/20/14:38
* @Description: AOP实现获取鉴权注解
*/
@Aspect
@Component
public class PermissionAspect {
//忽略
@Pointcut("execution(* com.xxx.yyy.controller.*.*(..))")
public void pointcut1(){
}
@Pointcut("@annotation(com.xxx.yyy.annotation.RolePermission)")
public void pointcut2(){
}
@Around("pointcut2()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//1.获取目标类上的目标注解(可判断目标类是否存在该注解)
RolePermission annotation = AnnotationUtils.findAnnotation(signature.getClass(), RolePermission.class);
//2.获取目标方法上的目标注解(可判断目标方法是否存在该注解)
RolePermission annotationInMethod = AnnotationUtils.findAnnotation(signature.getMethod(), RolePermission.class);
/*
或者:
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Class<?> tagClass = signatureInMethod.getDeclaringType();
boolean annotation = tagClass.isAnnotationPresent(HasPermission.class);
HasPermission annotationInClass=null;
if(annotation){
annotationInClass = tagClass.getAnnotation(HasPermission.class);
}
*/
//....
//具体业务逻辑
//....
return joinPoint.proceed(args);
}
}
2. 拦截器方式
package com.dj.springboot.study.interceptor;
import com.dj.springboot.study.annotation.RolePermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Author: ldj
* @Date: 2024/02/20/14:45
* @Description: 拦截器实现获取鉴权注解
*/
@Slf4j
@Component
public class PermissionInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
//1.获取目标类上的目标注解(可判断目标类是否存在该注解)
RolePermission annotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), RolePermission.class);
//2.获取目标方法上的目标注解(可判断目标方法是否存在该注解)
RolePermission annotationInMethod = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), RolePermission.class);
//..业务逻辑代码,如果返回false,表示鉴权失败,不放行
return false;
}
}
补充:鉴权注解定义
package com.dj.springboot.study.annotation;
import com.dj.springboot.study.enums.LogicalType;
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
/**
* @Author: ldj
* @Date: 2023/08/29/10:06
* @Description: 角色权限注解
*/
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RolePermission {
@AliasFor(value = "roles")
String[] value() default {};
@AliasFor(value = "value")
String[] roles() default {};
//逻辑条件: 与、或
LogicalType logical() default LogicalType.AND;
}