SpringBoot?AspectJ切面配合自定義注解實(shí)現(xiàn)權(quán)限校驗(yàn)的示例詳解
本文章介紹了如何通過創(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)先取方法上的注解,若方法上無注解,則取類上的注解
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)集合存儲到Session中,校驗(yàn)時通過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")
// 如果在類上使用注解,則對該類下所有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是一個用于存放權(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)容請搜索腳本之家以前的文章或繼續(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)文章
基于Spark實(shí)現(xiàn)隨機(jī)森林代碼
這篇文章主要為大家詳細(xì)介紹了基于Spark實(shí)現(xiàn)隨機(jī)森林代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
JavaWeb之Servlet注冊頁面的實(shí)現(xiàn)示例
注冊頁面是很多網(wǎng)站都會是使用的到,本文主要介紹了JavaWeb之Servlet注冊頁面的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Java使用BigDecimal公式精確計(jì)算及精度丟失問題
在工作中經(jīng)常會遇到數(shù)值精度問題,比如說使用float或者double的時候,可能會有精度丟失問題,下面這篇文章主要給大家介紹了關(guān)于Java使用BigDecimal公式精確計(jì)算及精度丟失問題的相關(guān)資料,需要的朋友可以參考下2023-01-01
springboot controller 增加指定前綴的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了springboot controller 增加指定前綴的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
初始JAVA模塊化開發(fā)的超詳細(xì)步驟(適合菜鳥)
這篇文章主要介紹了初始JAVA模塊化開發(fā)的超詳細(xì)步驟,詳細(xì)解釋了模塊描述符的職責(zé)、模塊路徑的概念以及如何使用命令行運(yùn)行模塊化Java程序,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03

