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; /** * 定義切點(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ā)那個發(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進(jìn)行全局日志記錄
- SpringBoot使用AOP,內(nèi)部方法失效的解決方案
- Springboot使用@Valid 和AOP做參數(shù)校驗及日志輸出問題
- 詳解基于SpringBoot使用AOP技術(shù)實現(xiàn)操作日志管理
- SpringBoot中使用AOP打印接口日志的方法
- SpringBoot項目中使用AOP的方法
- Springboot 中使用 Aop代碼實戰(zhàn)教程
相關(guān)文章
淺析Java中StringBuffer和StringBuilder的使用
當(dāng)對字符串進(jìn)行修改的時候,需要使用 StringBuffer 和 StringBuilder 類。本文就來和大家簡單聊聊這二者的使用與區(qū)別吧,希望對大家有所幫助2023-04-04細(xì)談java同步之JMM(Java Memory Model)
Java內(nèi)存模型是在硬件內(nèi)存模型上的更高層的抽象,它屏蔽了各種硬件和操作系統(tǒng)訪問的差異性,保證了Java程序在各種平臺下對內(nèi)存的訪問都能達(dá)到一致的效果。下面我們來一起學(xué)習(xí)下JMM2019-05-05Java Socket編程(三) 服務(wù)器Sockets
Java Socket編程(三) 服務(wù)器Sockets...2006-12-12