SpringBoot?AspectJ切面配合自定義注解實現權限校驗的示例詳解
本文章介紹了如何通過創(chuàng)建自定義的權限校驗注解,配合AspectJ切面攔截注解實現權限校驗。
1. 創(chuàng)建權限校驗注解
創(chuàng)建權限校驗注解,可用在方法和類上,authPoint屬性表示所需的權限點。代碼如下:
package com.guo.demo.examples.permissioncheck;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE, ElementType.METHOD})
public @interface PermissionCheck {
/**
* 所需的權限點
*
* @return 所需的權限點
*/
String authPoint();
}2. 創(chuàng)建AspectJ切面攔截注解校驗權限
創(chuàng)建AspectJ切面,攔截帶有@PermissionCheck注解的方法或類,獲取注解上的權限點進行校驗。代碼如下:
package com.guo.demo.examples.permissioncheck;
import lombok.extern.slf4j.Slf4j;
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;
import javax.annotation.Resource;
@Slf4j
@Aspect
@Component
public class PermissionCheckAspect {
// 權限校驗服務
@Resource
private PermissionService permissionService;
// 定義切入點表達式,匹配帶有PermissionCheck注解的方法或類
@Pointcut("@annotation(com.guo.demo.examples.permissioncheck.PermissionCheck) || @within(com.guo.demo.examples.permissioncheck.PermissionCheck)")
public void pointCut() {
}
@Around("pointCut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 1.獲取目標類上的目標注解
PermissionCheck annotationInClass = AnnotationUtils.findAnnotation(signature.getDeclaringType(), PermissionCheck.class);
// 2.獲取目標方法上的目標注解
PermissionCheck annotationInMethod = AnnotationUtils.findAnnotation(signature.getMethod(), PermissionCheck.class);
// 優(yōu)先取方法上的注解,若方法上無注解,則取類上的注解
PermissionCheck annotation = annotationInMethod != null ? annotationInMethod : annotationInClass;
if (annotation == null) {
log.error("PermissionCheck annotation is null, {}", signature.toLongString());
throw new RuntimeException("PermissionCheck annotation is null");
}
String authPoint = annotation.authPoint();
if (permissionService.hasAuthPoint(authPoint)) { // 進行權限校驗
return joinPoint.proceed();
} else {
log.warn("user [{}] no permission, authPoint: {}", SessionUtils.getCurrentUser().getFullName(), authPoint);
throw new RuntimeException("no permission: [" + authPoint + "]");
}
}
}PermissionService的hasAuthPoint方法用于判斷當前用戶是否擁有所需權限點。
例如,將用戶的擁有的權限點集合存儲到Session中,校驗時通過Session拿到用戶的權限點集合進行判斷
3. 用法示例
package com.guo.demo.examples.permissioncheck;
import com.guo.demo.pojo.vo.Response;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
@RestController
@RequestMapping("employee")
// 如果在類上使用注解,則對該類下所有public方法生效
// @PermissionCheck(authPoint = AuthPointConstant.EMPLOYEE_MANAGE)
public class EmployeeController {
@Resource
private EmployeeService employeeService;
@PermissionCheck(authPoint = AuthPointConstant.ADD_EMPLOYEE) // 在方法上使用校驗注解
@PostMapping("add")
public Response<?> add(@RequestBody @Valid AddEmployeeRequest request) {
employeeService.add(request);
return Response.success();
}
}AuthPointConstant是一個用于存放權限點常量的類,方便統(tǒng)一集中管理權限點。比如:
public class AuthPointConstant {
public static final String EMPLOYEE_MANAGE = "employee:manage"; // 管理員工
public static final String ADD_EMPLOYEE = "employee:add"; // 添加員工
}A. 參考文章
到此這篇關于SpringBoot AspectJ切面配合自定義注解實現權限校驗的示例詳解的文章就介紹到這了,更多相關SpringBoot AspectJ權限校驗內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Mybatis-Plus中使用@DS注解動態(tài)選擇數據源的源碼解讀
這篇文章主要介紹了Mybatis-Plus中使用@DS注解動態(tài)選擇數據源的源碼解讀,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
java_String和StringBuffer區(qū)別分析
JAVA平臺提供了兩個類:String和StringBuffer,它們可以儲存和操作字符串,即包含多個字符的字符數據。這個String類提供了數值不可改變的字符串。2013-04-04
Spring Boot Actuator未授權訪問漏洞的問題解決
Spring Boot Actuator 端點的未授權訪問漏洞是一個安全性問題,可能會導致未經授權的用戶訪問敏感的應用程序信息,本文就來介紹一下解決方法,感興趣的可以了解一下2023-09-09

