一、背景
前端管控的权限,可以通过设置菜单是否可见进行权限管控。后端该如何动态管控接口的权限,通过该问题,从网上搜索了一下,并通过自己的设计思路,出了一套方案。方案不是很成熟,希望各位大佬能给予指正。
二、具体实现
1、系统启动,预处理
项目启动扫描所有的controller类,及其所有接口地址、方法处理、入参和返回参数等。
系统启动,预处理的方法不太了解可以参考我的另外一篇博客:https://blog.csdn.net/Lin_Miao_09/article/details/108862768。
@Slf4j
@Order(value = 3)
@Component
public class ConfigApplicationRunner implements ApplicationRunner{
@Autowired
private ConfigurableApplicationContext run;
@Autowired
private ControllerApiMapper controllerApiMapper;
@Override
public void run(ApplicationArguments args) throws Exception {
//项目or模块名称
String appName = "test";
//数据实体类
ControllerApi controllerApi = null;
//获取restcontroller注解的类名
String[] beanNamesForAnnotation = run.getBeanNamesForAnnotation(RestController.class);
//获取类对象
for (String str : beanNamesForAnnotation) {
Object bean = run.getBean(str);
Class<?> forName = bean.getClass();
log.info(forName.getName());
//获取requestmapping注解的类
RequestMapping declaredAnnotation = forName.getAnnotation(RequestMapping.class);
String url_path = "";
if (declaredAnnotation != null) {
String[] value = (declaredAnnotation .value());
//获取类的url路径
url_path = value[0];
for (Method method : forName.getDeclaredMethods()) {
controllerApi = new ControllerApi();
//获取@GetMapping的方法
GetMapping annotation1 = method.getAnnotation(GetMapping.class);
//获取@PostMapping的方法
PostMapping annotation2 = method.getAnnotation(PostMapping.class);
//获取@PutMapping的方法
PutMapping annotation3 = method.getAnnotation(PutMapping.class);
//获取@DeleteMapping的方法
DeleteMapping annotation4 = method.getAnnotation(DeleteMapping.class);
//获取@RequestMapping的方法
RequestMapping annotation5 = method.getAnnotation(RequestMapping.class);
if (annotation1 != null) {
url_path += annotation1.value()[0];
log.info("方法路径" + url_path + "方法名" + method.getName()+"请求类型get请求参数"+JSONObject.toJSONString(method.getParameterTypes())+"返回参数"+method.getReturnType());
controllerApi.setAppName(appName);
controllerApi.setUrl(url_path);
controllerApi.setRequestType("get");
controllerApi.setName(method.getName());
controllerApi.setParameterTypes(JSONObject.toJSONString(method.getParameterTypes()));
controllerApi.setReturnType(method.getReturnType());
//默认启用接口
controllerApi.setStart(1);
}
if (annotation2 != null) {
url_path += annotation2.value()[0];
log.info("方法路径" + url_path + "方法名" + method.getName()+"请求类型post请求参数"+JSONObject.toJSONString(method.getParameterTypes())+"返回参数"+method.getReturnType());
controllerApi.setAppName(appName);
controllerApi.setUrl(url_path);
controllerApi.setRequestType("post");
controllerApi.setName(method.getName());
controllerApi.setParameterTypes(JSONObject.toJSONString(method.getParameterTypes()));
controllerApi.setReturnType(method.getReturnType());
//默认启用接口
controllerApi.setStart(1);
}
if (annotation3 != null) {
url_path += annotation3.value()[0];
log.info("方法路径" + url_path + "方法名" + method.getName()+"请求类型put请求参数"+JSONObject.toJSONString(method.getParameterTypes())+"返回参数"+method.getReturnType());
controllerApi.setAppName(appName);
controllerApi.setUrl(url_path);
controllerApi.setRequestType("put");
controllerApi.setName(method.getName());
controllerApi.setParameterTypes(JSONObject.toJSONString(method.getParameterTypes()));
controllerApi.setReturnType(method.getReturnType());
//默认启用接口
controllerApi.setStart(1);
}
if (annotation4 != null) {
url_path += annotation4.value()[0];
log.info("方法路径" + url_path + "方法名" + method.getName()+"请求类型delete请求参数"+JSONObject.toJSONString(method.getParameterTypes())+"返回参数"+method.getReturnType());
controllerApi.setAppName(appName);
controllerApi.setUrl(url_path);
controllerApi.setRequestType("delete");
controllerApi.setName(method.getName());
controllerApi.setParameterTypes(JSONObject.toJSONString(method.getParameterTypes()));
controllerApi.setReturnType(method.getReturnType());
//默认启用接口
controllerApi.setStart(1);
}
if (annotation5 != null) {
url_path += annotation4.value()[0];
log.info("方法路径" + url_path + "方法名" + method.getName()+"请求参数"+JSONObject.toJSONString(method.getParameterTypes())+"返回参数"+method.getReturnType());
controllerApi.setAppName(appName);
controllerApi.setUrl(url_path);
controllerApi.setName(method.getName());
controllerApi.setParameterTypes(JSONObject.toJSONString(method.getParameterTypes()));
controllerApi.setReturnType(method.getReturnType());
//默认启用接口
controllerApi.setStart(1);
}
url_path = value[0];
//入库
controllerApiMapper.insert(controllerApi);
}
}
}
}
}
可以通过appName、url、start三者来对接口进行管控,是否启用接口。