Click on the top [ full-stack developer community ] → upper right corner [...] → [set to star ⭐ ]
Source: http: //rrd.me/epDWH
Foreword
use
Auth.java
UserController.java
The main function
The main function code is complete
Foreword
In recent permission to do the project, using shiro achieve restful rights management interfaces, the entire project has been restructured. The need to use all of the rights management interface configuration, including interfaces url address, interface unique code and so on. I want to collect all the interface information, if a lot of engineering interfaces, the workload can be imagined.
Here with the reflection, to get all the information interface, the interface more, not a few seconds to do.
use
Auth.java
Interface information objects
Including authorization address, uniquely identifies the authority, permission name, creation time, request method
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
The user interface for the test interface.
This uses standard interfaces restful style, swagger automatic API interface, shiro interfaces authority notes @RequiresPermissions combined into a controller. Of course, other techniques may also be used, as long as the interface to obtain information on the line.
Notes is not important where the information is annotated.
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();
}
}
The main function
Here by reflection, obtaining a description of all interfaces UserController and stored in the database. This is the main class.
1. package scan path setting
Reflections reflections =
new Reflections(new ConfigurationBuilder().
setUrls(ClasspathHelper.
forPackage(scanPackage)).
setScanners(new MethodAnnotationsScanner()));
2. The method of obtaining the all annotated with the scanning @RequiresPermissions package collection
Set<Method> methods = reflections.getMethodsAnnotatedWith(RequiresPermissions.class);
3. acquiring annotation category by reflection on
method.getDeclaringClass().getAnnotation(RequestMapping.class);
4. The method of obtaining the annotation by reflection
method.getAnnotation(PutMapping.class);
5. Gets a property in annotations (here is to get the value attribute)
method.getAnnotation(PutMapping.class).value();
The main function code is complete
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));
}
}
It can be obtained by the method mentioned above in the notes to the value, so that you can get to the interface information we want, and the results are as follows