SpringBoot使用AOP實現(xiàn)統(tǒng)一角色權限校驗
一、引入AOP starter
在tg-book-common中引入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>對于spring boot的starter,我在之前的文章中已經(jīng)反復說明過多次,不做贅述!
本項目中已經(jīng)使用中的starter如下:
- spring-boot-starter-web
- spring-boot-starter-logging
- mybatis-spring-boot-starter
- pagehelper-spring-boot-starter
二、創(chuàng)建切面@Aspect + 定義切點@Pointcut
@Aspect注解方式,它的概念像@Aspect、@Pointcut、@Before、@After、@Around等注解都是來自于 AspectJ,但是功能的實現(xiàn)是純 Spring AOP 自己實現(xiàn)的,主要有兩大核心:
- 定義[切入點]:使用 @Pointcut 切點表達式,你可以理解成類似于正則表達式的強大東東。(例如本文的@annotation方式)
- 定義[切入時機] 和 [增強處理邏輯]:五種通知Advice注解 對[切入點]執(zhí)行增強處理, 包括:@Before、@After、@AfterRunning、@AfterThrowing、@Around
以下使用@Aspect 定義一個切面類,使用@Pointcut定義一個切點,切點表達式使用@annotation方式,也就是注解的方式。
// @Aspect和@Component定義一個切面類,@Slf4j是之前講過的日志注解
@Component
@Aspect
@Slf4j
public class RoleAspect {
// 核心一:定義切點(使用@annotation方式)
@Pointcut(value = "@annotation( org.tg.book.common.annotation.Role)")
public void pointCut() {}
}
三、封裝校驗@Role角色權限的方法
本文的AOP是上文攔截器Interceptor的另一種實現(xiàn)方式,所以請將上文的AuthInterceptor中的如下代碼注釋:

然后把這段代碼拿過來,封裝成一個方法,放到RoleAspect 中如下:
/**
* 將@Role與登錄用戶的角色對比,如果是管理員返回true
**/
private boolean checkAdminRole(Role role) {
// 校驗角色
if (role != null) {
// 走到這,說明方法上加了@Role
boolean isAdmin = false;
AuthContextInfo authInfo = AuthContextInfo.getAuthInfo();
for (int roleId : role.roleIds()) {
if (authInfo.getRoleId().equals(roleId)) {
isAdmin = true;
break;
}
}
if (!isAdmin) {
log.info("[403]無權限, authInfo={}", authInfo);
return false;
}
}
return true;
}方法邏輯很簡單:將@Role與登錄用戶的角色對比,如果是管理員返回true,否則返回false
四、AOP兩種實現(xiàn)方式
4.1 前置通知@Before方式
因為角色權限校驗代碼,發(fā)生于【業(yè)務方法代碼】之前,所以可以使用前置通知@Before方式,代碼如下:
@Before("pointCut()")
public void before(JoinPoint joinPoint) throws NoSuchMethodException {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Class<?> clazz = joinPoint.getTarget().getClass();
Method method = clazz.getMethod(signature.getName(), signature.getParameterTypes());
Role role = method.getAnnotation(Role.class);
boolean isAdminRole = checkAdminRole(role);
if (!isAdminRole) {
throw new RuntimeException("無權限");
}
}核心邏輯是獲得@Role注解,然后進行校驗,如果非管理員,則
拋出異常。這里實現(xiàn)的比較簡單,當后面我們實現(xiàn)了【全局異常處理】以后,這里就可以換成自定義的異常類,交給【全局異常處理】統(tǒng)一處理!
4.2 環(huán)繞通知@Around方式
如果不拋出異常的話,如何處理?
可以使用@Around方式,環(huán)繞通知@Around可以控制在【業(yè)務方法代碼】之前校驗,并且可以返回結果,所以我們就不需要拋出異常了!
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Class<?> clazz = joinPoint.getTarget().getClass();
Method method = clazz.getMethod(signature.getName(), signature.getParameterTypes());
Role role = method.getAnnotation(Role.class);
boolean isAdminRole = checkAdminRole(role);
if (!isAdminRole) {
return TgResult.fail("403", "無權限");
}
return joinPoint.proceed();
}獲取Role 之前的代碼都是一模一樣的,區(qū)別就是這里沒有拋出異常,而是返回統(tǒng)一結果TgResult,這也正是封裝統(tǒng)一返回結果的好處之一?。?!
特別注意: before和around是兩種實現(xiàn)方式,所以不必在意從joinPoint得到role的重復代碼,因為最終只會寫一份代碼,對于before和around我更建議使用around的方式!
以上就是SpringBoot使用AOP實現(xiàn)統(tǒng)一角色權限校驗的詳細內容,更多關于SpringBoot AOP統(tǒng)一角色權限校驗的資料請關注腳本之家其它相關文章!
相關文章
SpringBoot攔截器實現(xiàn)登錄攔截的示例代碼
本文主要介紹了SpringBoot攔截器實現(xiàn)登錄攔截,文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Java實現(xiàn)動態(tài)規(guī)劃背包問題
本文主要介紹使用java實現(xiàn)動態(tài)規(guī)劃的背包問題,詳細使用圖文和多種案例進行解析,幫助理解該算法2021-06-06
關于Spring多數(shù)據(jù)源TransactionManager沖突的解決方案
這篇文章主要介紹了關于Spring多數(shù)據(jù)源TransactionManager沖突的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring Boot如何使用JDBC獲取相關的數(shù)據(jù)詳解
這篇文章主要給大家介紹了關于Spring Boot如何使用JDBC獲取相關數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-03-03
SpringBoot中的@EnableConfigurationProperties注解原理及用法
在SpringBoot中,@EnableConfigurationProperties注解是一個非常有用的注解,它可以用于啟用對特定配置類的支持,在本文中,我們將深入探討@EnableConfigurationProperties注解,包括它的原理和如何使用,需要的朋友可以參考下2023-06-06
springboot引入遠程nacos配置文件錯誤的解決方案
本文為解決Spring Cloud Alibaba中配置導入問題,提供了詳細的步驟說明,包括引入依賴、配置nacos、創(chuàng)建bootstrap.yml文件以及測試配置導入是否成功的方法,幫助開發(fā)者快速解決相關問題2024-09-09

