通常在項目開發中會根據登陸人員的權限大小對接口也會設置權限,那麼對接口權限是怎麼實現的呢,大多數都是用自定義權限註解,只須要在接口上加上一個註解就能夠實現對接口的權限攔截,是否對該接口有權調用java
接下來咱們用一個簡單的案例測試一下如何實現自定義權限註解web
一、首先,建立一個類,命名隨意,這裏爲MyPermission spring
package com.study.permission; import java.lang.annotation.*; @Documented //做用域 @Inherited //可繼承 @Target(ElementType.METHOD)//標明自定義註解可做用的地方,指方法 @Retention(RetentionPolicy.RUNTIME) //存活階段,RUNRIME:存在運行期,還有jvm,class文件級別 public @interface MyPermission { String username() default "name"; //是否須要數據權限,默認爲true boolean required() default true; }
在此解釋一下,以上類中出現的註解json
●@Target註解app
●@Retention註解jvm
Reteniton註解的做用是:描述註解保留的時間範圍(被描述的註解在它所修飾的類中能夠被保留到什麼時候)ide
Reteniton註解用來限定那些被它所註解的註解類在註解到其餘類上之後,可被保留到什麼時候,一共有三種策略(SOURCE,CLASS,RUNTIME),定義在RetentionPolicy枚舉中工具
●@Documented註解
Documented註解的做用是:描述在使用 javadoc 工具爲類生成幫助文檔時是否要保留其註解信息,有興趣的看官本身去研究研究post
●@Inherited註解
Inherited註解的做用是:使被它修飾的註解具備繼承性(若是某個類使用了被@Inherited修飾的註解,則其子類將自動具備該註解),有興趣的看官本身去研究研究測試
二、建立一個類MyPermissionAspect並繼承Ordered
package com.study.permission; 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.Ordered; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; import java.lang.reflect.Method; @Component @Aspect public class MyPermissionAspect implements Ordered { @Pointcut("execution(* com.study.controller..*(..))") public void permissionTest() { } @Around("permissionTest()") public Object doPermission(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature)joinPoint.getSignature(); Method method = signature.getMethod(); MyPermission myPermission = method.getAnnotation(MyPermission.class); if(myPermission == null){ return joinPoint.proceed(); } //判斷是否須要數據權限 boolean required = myPermission.required(); if (!required) { return joinPoint.proceed(); } Object[] args = joinPoint.getArgs(); if(null == args || args.length == 0){ return "參數爲空"; } JSONObject json = JSONObject.parseObject(String.valueOf(args[0])); String username = json.getString(myPermission.username()); if(!"admin".equals(username)){ return "權限驗證未經過"; } return joinPoint.proceed(); } @Override public int getOrder() { return 0; } }
三、測試demo,在controller層寫測試方法
package com.study.controller; import com.study.permission.MyPermission; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class MainController { @RequestMapping("test") @MyPermission public String permissionTest(@RequestBody String name){ System.out.println("已經過權限"); return "success"; } @RequestMapping("test02") public String permissionTest02(@RequestBody String name){ System.out.println("未加權限"); return "success"; } }
在test方法上加權限註解@MyPermission,用postman測試後,傳入參數name進行判斷攔截,若是是admin則返回success,若是非admin,則顯示權限驗證未經過
加上註解,但傳的參數爲非admin時,顯示不經過
測試方法二test02,不加權限註解時,接口應該返回success
寫完了,大體內容就是這樣紫了,可是通常狀況下進行權限驗證時候會攜帶token的,經過request獲取token,而後根據token去查詢判斷當前用戶的信息,再進行邏輯判斷,這裏所有省略了。。。