SpringBoot?AspectJ切面配合自定義注解實(shí)現(xiàn)權(quán)限校驗(yàn)的示例詳解
本文章介紹了如何通過(guò)創(chuàng)建自定義的權(quán)限校驗(yàn)注解,配合AspectJ切面攔截注解實(shí)現(xiàn)權(quán)限校驗(yàn)。
1. 創(chuàng)建權(quán)限校驗(yàn)注解
創(chuàng)建權(quán)限校驗(yàn)注解,可用在方法和類上,authPoint屬性表示所需的權(quán)限點(diǎn)。代碼如下:
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 {
/**
* 所需的權(quán)限點(diǎn)
*
* @return 所需的權(quán)限點(diǎn)
*/
String authPoint();
}2. 創(chuàng)建AspectJ切面攔截注解校驗(yàn)權(quán)限
創(chuàng)建AspectJ切面,攔截帶有@PermissionCheck注解的方法或類,獲取注解上的權(quán)限點(diǎn)進(jìn)行校驗(yàn)。代碼如下:
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 {
// 權(quán)限校驗(yàn)服務(wù)
@Resource
private PermissionService permissionService;
// 定義切入點(diǎn)表達(dá)式,匹配帶有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.獲取目標(biāo)類上的目標(biāo)注解
PermissionCheck annotationInClass = AnnotationUtils.findAnnotation(signature.getDeclaringType(), PermissionCheck.class);
// 2.獲取目標(biāo)方法上的目標(biāo)注解
PermissionCheck annotationInMethod = AnnotationUtils.findAnnotation(signature.getMethod(), PermissionCheck.class);
// 優(yōu)先取方法上的注解,若方法上無(wú)注解,則取類上的注解
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)) { // 進(jìn)行權(quán)限校驗(yàn)
return joinPoint.proceed();
} else {
log.warn("user [{}] no permission, authPoint: {}", SessionUtils.getCurrentUser().getFullName(), authPoint);
throw new RuntimeException("no permission: [" + authPoint + "]");
}
}
}PermissionService的hasAuthPoint方法用于判斷當(dāng)前用戶是否擁有所需權(quán)限點(diǎn)。
例如,將用戶的擁有的權(quán)限點(diǎn)集合存儲(chǔ)到Session中,校驗(yàn)時(shí)通過(guò)Session拿到用戶的權(quán)限點(diǎn)集合進(jìn)行判斷
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")
// 如果在類上使用注解,則對(duì)該類下所有public方法生效
// @PermissionCheck(authPoint = AuthPointConstant.EMPLOYEE_MANAGE)
public class EmployeeController {
@Resource
private EmployeeService employeeService;
@PermissionCheck(authPoint = AuthPointConstant.ADD_EMPLOYEE) // 在方法上使用校驗(yàn)注解
@PostMapping("add")
public Response<?> add(@RequestBody @Valid AddEmployeeRequest request) {
employeeService.add(request);
return Response.success();
}
}AuthPointConstant是一個(gè)用于存放權(quán)限點(diǎn)常量的類,方便統(tǒng)一集中管理權(quán)限點(diǎn)。比如:
public class AuthPointConstant {
public static final String EMPLOYEE_MANAGE = "employee:manage"; // 管理員工
public static final String ADD_EMPLOYEE = "employee:add"; // 添加員工
}A. 參考文章
到此這篇關(guān)于SpringBoot AspectJ切面配合自定義注解實(shí)現(xiàn)權(quán)限校驗(yàn)的示例詳解的文章就介紹到這了,更多相關(guān)SpringBoot AspectJ權(quán)限校驗(yàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot AOP AspectJ切面技術(shù)介紹與實(shí)現(xiàn)方式
- Springboot如何使用Aspectj實(shí)現(xiàn)AOP面向切面編程
- SpringBoot中給指定接口加上權(quán)限校驗(yàn)的實(shí)現(xiàn)
- SpringBoot切面實(shí)現(xiàn)token權(quán)限校驗(yàn)詳解
- SpringBoot使用AOP實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn)
- SpringBoot使用攔截器Interceptor實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn)
- SpringBoot?使用?Sa-Token?完成注解鑒權(quán)功能(權(quán)限校驗(yàn))
相關(guān)文章
Mybatis-Plus中使用@DS注解動(dòng)態(tài)選擇數(shù)據(jù)源的源碼解讀
這篇文章主要介紹了Mybatis-Plus中使用@DS注解動(dòng)態(tài)選擇數(shù)據(jù)源的源碼解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
java&javascript自定義加密數(shù)據(jù)傳輸代碼示例
這篇文章主要介紹了java&javascript自定義加密數(shù)據(jù)傳輸代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
關(guān)于在IDEA熱部署插件JRebel使用問(wèn)題詳解
這篇文章主要介紹了關(guān)于在IDEA熱部署插件JRebel使用問(wèn)題詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
java_String和StringBuffer區(qū)別分析
JAVA平臺(tái)提供了兩個(gè)類:String和StringBuffer,它們可以儲(chǔ)存和操作字符串,即包含多個(gè)字符的字符數(shù)據(jù)。這個(gè)String類提供了數(shù)值不可改變的字符串。2013-04-04
spring boot 2整合swagger-ui過(guò)程解析
這篇文章主要介紹了spring boot 2整合swagger-ui過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Spring Boot Actuator未授權(quán)訪問(wèn)漏洞的問(wèn)題解決
Spring Boot Actuator 端點(diǎn)的未授權(quán)訪問(wèn)漏洞是一個(gè)安全性問(wèn)題,可能會(huì)導(dǎo)致未經(jīng)授權(quán)的用戶訪問(wèn)敏感的應(yīng)用程序信息,本文就來(lái)介紹一下解決方法,感興趣的可以了解一下2023-09-09
java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

