SpringBoot使用AOP+注解實現(xiàn)簡單的權(quán)限驗證的方法
SpringAOP的介紹:傳送門
demo介紹
主要通過自定義注解,使用SpringAOP的環(huán)繞通知攔截請求,判斷該方法是否有自定義注解,然后判斷該用戶是否有該權(quán)限。這里做的比較簡單,只有兩個權(quán)限:一個普通用戶、一個管理員。
項目搭建
這里是基于SpringBoot的,對于SpringBoot項目的搭建就不說了。在項目中添加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;
/**
* 定義切點
*/
@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ā)那個發(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);
}
}
}
}
使用
只需要在需要驗證的方法上添加自定義注解: @Permission既可
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot使用自定義注解實現(xiàn)aop切面日志
- SpringBoot使用AOP記錄接口操作日志詳解
- SpringBoot使用AOP實現(xiàn)統(tǒng)計全局接口訪問次數(shù)詳解
- 在springboot中使用AOP進行全局日志記錄
- SpringBoot使用AOP,內(nèi)部方法失效的解決方案
- Springboot使用@Valid 和AOP做參數(shù)校驗及日志輸出問題
- 詳解基于SpringBoot使用AOP技術(shù)實現(xiàn)操作日志管理
- SpringBoot中使用AOP打印接口日志的方法
- SpringBoot項目中使用AOP的方法
- Springboot 中使用 Aop代碼實戰(zhàn)教程
相關(guān)文章
springboot web項目打jar或者war包并運行的實現(xiàn)
這篇文章主要介紹了springboot web項目打jar或者war包并運行的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java中使用jaxp進行sax解析_動力節(jié)點Java學(xué)院整理
使用SAX的優(yōu)勢在于其解析速度較快,相對于DOM而言占用內(nèi)存較少。這篇文章主要介紹了Java中使用jaxp進行sax解析,需要的朋友可以參考下2017-08-08
解決IDEA報錯,無效的源發(fā)行版 無效的目標(biāo)發(fā)行版:22問題
在項目編譯過程中,可能會出現(xiàn)“無效的源發(fā)行版”或“無效的目標(biāo)發(fā)行版”的報錯信息,原因通常是編譯使用的JDK版本與項目設(shè)置的發(fā)布版本不一致,解決這類問題的辦法是統(tǒng)一JDK版本,具體操作為:在IDE的項目設(shè)置中(如File->ProjectStructure->ProjectSettings)2024-10-10

