原文地址:https://blog.csdn.net/m0_37459380/article/details/86568878
前言
最近在做項目權限,使用shiro實現restful接口權限管理,對整個項目都進行了重構。而權限管理需要用到所有的接口配置,包括接口url地址,接口唯一編碼等。想要收集所有的接口信息,如果工程接口很多,工作量可想而知。
這裏用了反射,來獲取所有接口的信息,接口再多,也不過幾秒鐘的事。
使用
Auth.java
接口信息對象
主要包括授權地址,權限唯一標識,權限名稱,創建時間,請求方式
package com.wwj.springboot.model;
import java.io.Serializable;
import java.util.Date;
public class Auth implements Serializable {
private String authName;
private String authUrl;
private String authUniqueMark;
private Date createTime;
private String methodType;
//get set 省略
}
UserController.java
用戶接口
用於測試的接口。
這裏使用了標準的restful接口風格,swagger自動API接口,shiro 接口權限註解@RequiresPermissions組合成的一個controller。當然也可以使用其他技術,只要能獲取到接口信息就行。
註解不重要,重要的是註解裏的信息。
package com.wwj.springboot.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
@Api(value = "用戶管理", tags = {"用戶管理"})
public class UserController {
@GetMapping
@ApiOperation("獲取列表")
@RequiresPermissions("user:list")
public void list() {
System.out.println();
}
@GetMapping(path = "/{userId}")
@ApiOperation("獲取詳情")
@RequiresPermissions("user:get")
public void getUserById(@PathVariable("userId") String userId) {
System.out.println();
}
@PostMapping
@ApiOperation("新增一個用戶")
@RequiresPermissions("user:save")
public void save() {
System.out.println();
}
@PutMapping("/{userId}")
@ApiOperation("修改保存")
@RequiresPermissions("user:update")
public void editSave(@PathVariable String userId) {
System.out.println();
}
}
主函數
這裏通過反射,獲取了UserController的所有接口的說明,並存入數據庫中。這是最主要的類。
1.設置掃描的package路徑
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(scanPackage)).setScanners(new MethodAnnotationsScanner()));
2.獲取到掃描包內帶有@RequiresPermissions註解的所有方法集合
Set<Method> methods = reflections.getMethodsAnnotatedWith(RequiresPermissions.class);
3.通過反射獲取類上的註解
method.getDeclaringClass().getAnnotation(RequestMapping.class)
4.通過反射獲取方法上的註解
method.getAnnotation(PutMapping.class);
5.獲取註解中的某個屬性(這裏是獲取value屬性)
完整的主函數代碼
package com.wwj.springboot;
import com.alibaba.fastjson.JSON;
import com.wwj.springboot.model.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
public class AnnoTest {
public static void main(String[] args) {
getRequestMappingMethod("com.wwj.springboot.controller");
}
/**
* @param scanPackage 需要掃描的包路徑
*/
private static void getRequestMappingMethod(String scanPackage) {
//設置掃描路徑
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(scanPackage)).setScanners(new MethodAnnotationsScanner()));
//掃描包內帶有@RequiresPermissions註解的所有方法集合
Set<Method> methods = reflections.getMethodsAnnotatedWith(RequiresPermissions.class);
List<Auth> list = new ArrayList<>();
Date now = new Date();
//循環獲取方法
methods.forEach(method -> {
//用於保存方法的請求類型
String methodType = "";
//獲取類上的@RequestMapping註解的值,作爲請求的基礎路徑
String authUrl = method.getDeclaringClass().getAnnotation(RequestMapping.class).value()[0];
//獲取方法上的@PutMapping,@GetMapping,@PostMapping,@DeleteMapping註解的值,作爲請求路徑,並區分請求方式
if (method.getAnnotation(PutMapping.class) != null) {
methodType = "put";
if (method.getAnnotation(PutMapping.class).value().length > 0) {
authUrl = method.getAnnotation(PutMapping.class).value()[0];
}
} else if (method.getAnnotation(GetMapping.class) != null) {
methodType = "get";
if (method.getAnnotation(GetMapping.class).value().length > 0) {
authUrl = method.getAnnotation(GetMapping.class).value()[0];
}
} else if (method.getAnnotation(PostMapping.class) != null) {
methodType = "post";
if (method.getAnnotation(PostMapping.class).value().length > 0) {
authUrl = method.getAnnotation(PostMapping.class).value()[0];
}
} else if (method.getAnnotation(DeleteMapping.class) != null) {
if (method.getAnnotation(DeleteMapping.class).value().length > 0) {
authUrl = method.getAnnotation(DeleteMapping.class).value()[0];
}
}
//使用Auth對象來保存值
Auth auth = new Auth();
auth.setMethodType(methodType);
auth.setAuthUniqueMark(method.getAnnotation(RequiresPermissions.class).value()[0]);
auth.setAuthUrl(authUrl);
auth.setAuthName(method.getDeclaringClass().getAnnotation(Api.class).value() + "-" + method.getAnnotation(ApiOperation.class).value());
auth.setCreateTime(now);
list.add(auth);
});
//TODO 輸出到控制檯,此處存數據庫即可
System.out.println(JSON.toJSONString(list));
}
}
method.getAnnotation(PutMapping.class).value();
通過上面所說的方法即可獲取到註解中的值,這樣就可以獲取到我們想要的接口信息了,執行結果如下
[{"authName":"用戶管理-獲取詳情","authUniqueMark":"user:get","authUrl":"/users","createTime":1540977757616,"methodType":"get"},
{"authName":"用戶管理-新增一個用戶","authUniqueMark":"user:save","authUrl":"/users","createTime":1540977757616,"methodType":"post"},
{"authName":"用戶管理-修改保存","authUniqueMark":"user:update","authUrl":"/{userId}","createTime":1540977757616,"methodType":"put"},
{"authName":"用戶管理-獲取列表","authUniqueMark":"user:list","authUrl":"/users","createTime":1540977757616,"methodType":"get"}]
感謝您的閱讀,如果您覺得閱讀本文對您有幫助,請點一下“推薦”按鈕。本文歡迎各位轉載,但是轉載文章之後必須在文章開頭給出原文鏈接。