SpringBoot使用AOP+注解實(shí)現(xiàn)簡(jiǎn)單的權(quán)限驗(yàn)證的方法
SpringAOP的介紹:傳送門
demo介紹
主要通過自定義注解,使用SpringAOP的環(huán)繞通知攔截請(qǐng)求,判斷該方法是否有自定義注解,然后判斷該用戶是否有該權(quán)限。這里做的比較簡(jiǎn)單,只有兩個(gè)權(quán)限:一個(gè)普通用戶、一個(gè)管理員。
項(xiàng)目搭建
這里是基于SpringBoot的,對(duì)于SpringBoot項(xiàng)目的搭建就不說了。在項(xiàng)目中添加AOP的依賴:<!--more--->
<!--AOP包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
自定義注解及解析
在方法上添加該注解,說明該方法需要管理員權(quán)限才能訪問。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Permission {
String authorities() default "ADMIN";
}
解析類:通過AOP的環(huán)繞通知獲取方法上的注解,判斷是否有Permission注解,返回注解的值。
public class AnnotationParse {
/***
* 解析權(quán)限注解
* @return 返回注解的authorities值
* @throws Exception
*/
public static String privilegeParse(Method method) throws Exception {
//獲取該方法
if(method.isAnnotationPresent(Permission.class)){
Permission annotation = method.getAnnotation(Permission.class);
return annotation.authorities();
}
return null;
}
}
SpringAOP環(huán)繞通知
@Aspect
@Component
public class ControllerAspect {
private final static Logger logger = LoggerFactory.getLogger(ControllerAspect.class);
@Autowired
private UserService userService;
/**
* 定義切點(diǎn)
*/
@Pointcut("execution(public * com.wqh.blog.controller.*.*(..))")
public void privilege(){}
/**
* 權(quán)限環(huán)繞通知
* @param joinPoint
* @throws Throwable
*/
@ResponseBody
@Around("privilege()")
public Object isAccessMethod(ProceedingJoinPoint joinPoint) throws Throwable {
//獲取訪問目標(biāo)方法
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
//得到方法的訪問權(quán)限
final String methodAccess = AnnotationParse.privilegeParse(targetMethod);
//如果該方法上沒有權(quán)限注解,直接調(diào)用目標(biāo)方法
if(StringUtils.isBlank(methodAccess)){
return joinPoint.proceed();
}else {
//獲取當(dāng)前用戶的權(quán)限,這里是自定義的發(fā)那個(gè)發(fā)
User currentUser = userService.getCurrentUser();
logger.info("訪問用戶,{}",currentUser.toString());
if(currentUser == null){
throw new LoginException(ResultEnum.LOGIN_ERROR);
}
if(methodAccess.equals(currentUser.getRole().toString())){
return joinPoint.proceed();
}else {
throw new BusinessException(ResultEnum.ROLE_ERROR);
}
}
}
}
使用
只需要在需要驗(yàn)證的方法上添加自定義注解: @Permission既可
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot中使用AOP打印接口日志的方法
- SpringBoot中創(chuàng)建的AOP不生效的原因及解決
- springboot?aop里的@Pointcut()的配置方式
- SpringBoot AOP方式實(shí)現(xiàn)多數(shù)據(jù)源切換的方法
- SpringBoot項(xiàng)目中使用AOP的方法
- 詳解基于SpringBoot使用AOP技術(shù)實(shí)現(xiàn)操作日志管理
- springboot配置aop切面日志打印過程解析
- SpringBoot使用AOP實(shí)現(xiàn)統(tǒng)計(jì)全局接口訪問次數(shù)詳解
- SpringBoot中使用AOP實(shí)現(xiàn)日志記錄功能
- SpringBoot中AOP的多種用途與實(shí)踐指南
相關(guān)文章
Java與MySQL導(dǎo)致的時(shí)間不一致問題分析
在使用MySQL的過程中,你可能會(huì)遇到時(shí)區(qū)相關(guān)問題,本文主要介紹了Java與MySQL導(dǎo)致的時(shí)間不一致問題分析,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Java C++實(shí)現(xiàn)相同MD5加密算法的方式
這篇文章主要介紹了Java與C++實(shí)現(xiàn)相同MD5加密算法的方法,需要的朋友可以參考下面文章內(nèi)容2021-09-09
JAVA后臺(tái)轉(zhuǎn)換成樹結(jié)構(gòu)數(shù)據(jù)返回給前端的實(shí)現(xiàn)方法
這篇文章主要介紹了JAVA后臺(tái)轉(zhuǎn)換成樹結(jié)構(gòu)數(shù)據(jù)返回給前端的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Spring MVC溫故而知新系列教程之請(qǐng)求映射RequestMapping注解
這篇文章主要介紹了Spring MVC溫故而知新系列教程之請(qǐng)求映射RequestMapping注解的相關(guān)知識(shí),文中給大家介紹了RequestMapping注解提供的幾個(gè)屬性及注解說明,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05
解決springboot項(xiàng)目找不到resources目錄下的資源問題
這篇文章主要介紹了解決springboot項(xiàng)目找不到resources目錄下的資源問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

