使用RequestMappingHandlerMapping.getHandlerMethods()
在做shiro权限控制的时候,刚开始添加权限模块没有做好,只能每次手动去给数据库添加权限数据,此时就可以利用一下代码思路去让系统调用该接口去扫描所有Controller接口上所有的注解附带的权限数据去插入到数据库
@Autowired
private RequestMappingHandlerMapping rMhm;
@ResponseBody
@RequestMapping("listMethods")
@PermissionName("哈哈哈哈")
public void listMethods() {
Map<RequestMappingInfo, HandlerMethod> handlermethod= rMhm.getHandlerMethods();
Collection< HandlerMethod> methods = handlermethod.values();
for (HandlerMethod method : methods) {
RequiresPermissions rPermissions = method.getMethodAnnotation(RequiresPermissions.class);
if (rPermissions != null) {
String resource = rPermissions.value()[0];
PermissionName permissionName = method.getMethodAnnotation(PermissionName.class);
if (permissionName != null) {
String name = permissionName.value();
Permission p = new Permission();
p.setResource(resource);
p.setName(name);
permissionDAO.save(p);
}
}
}
}
上面代码需要自己把数据库已有权限数据筛选掉
自定一个注解permessname
注解内容
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface permessname {
String value();
}
ending…