1 /** 2 * 获取项目所有被注解修饰的url 3 * @param run 4 */ 5 public void getAllUrl(ConfigurableApplicationContext run) { 6 //获取restcontroller注解的类名 7 String[] beanNamesForAnnotation = run.getBeanNamesForAnnotation(RestController.class); 8 //获取类对象 9 for (String str : beanNamesForAnnotation) { 10 Object bean = run.getBean(str); 11 12 //处理被AOP关照了的Controller 13 while (AopUtils.isAopProxy(bean)) { 14 try { 15 bean = ((Advised) bean).getTargetSource().getTarget(); 16 } catch (Exception e) { 17 throw new RuntimeException("get target bean failed", e); 18 } 19 } 20 21 Class<?> forName = bean.getClass(); 22 RequestMapping declaredAnnotation = forName.getAnnotation(RequestMapping.class); 23 if (declaredAnnotation != null) { 24 //获取类URL 25 String urlStarts = declaredAnnotation.value()[0]; 26 //处理方法URL 27 if(urlStarts == null) { 28 urlStarts = "/"+str+"/"; 29 } 30 if(!urlStarts.startsWith("/")) {//处理前缀 31 urlStarts = "/"+urlStarts; 32 } 33 if(!urlStarts.endsWith("/")) {//处理后缀 34 urlStarts = urlStarts+"/"; 35 } 36 for (Method method : forName.getDeclaredMethods()) { 37 //过滤没有添加 @IgnoreLogin的请求 38 IgnoreLogin ignoreLogin = method.getAnnotation(IgnoreLogin.class); 39 if(ignoreLogin == null) {continue; } 40 //获取方法URL 41 String urlEnd = "".intern(); 42 for(;;) {// 暂时就这些,需要再加 43 RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); 44 if (requestMapping != null) { 45 urlEnd = requestMapping.value()[0]; 46 break; 47 } 48 GetMapping getMapping = method.getAnnotation(GetMapping.class); 49 if (getMapping != null) { 50 urlEnd = getMapping.value()[0]; 51 break; 52 } 53 PostMapping postMapping = method.getAnnotation(PostMapping.class); 54 if (postMapping != null) { 55 urlEnd = postMapping.value()[0]; 56 break; 57 } 58 PutMapping putMapping = method.getAnnotation(PutMapping.class); 59 if (putMapping != null) { 60 urlEnd = putMapping.value()[0]; 61 break; 62 } 63 DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class); 64 if (deleteMapping != null) { 65 urlEnd = deleteMapping.value()[0]; 66 break; 67 } 68 } 69 //处理方法URL 70 if(urlEnd == null) { 71 urlEnd = method.getName(); 72 } 73 if(urlEnd.startsWith("/")) {//处理前缀 74 urlEnd = urlEnd.substring(1); 75 } 76 //处理 urlEnd 的 {} 不考虑 {123}id 的情况 77 if(urlEnd.indexOf("{")>=0) { 78 String[] split = urlEnd.split("/"); 79 for (String s : split) { 80 if(s.startsWith("{")) { 81 s = "*"; 82 } 83 } 84 urlEnd = String.join("/",split); 85 } 86 //存入请求忽略列表 87 if(StringUtils.isEmpty(properties.getAnonUris())) { 88 properties.setAnonUris(urlStarts+urlEnd); 89 } else { 90 properties.setAnonUris(properties.getAnonUris()+StringConstant.COMMA+urlStarts+urlEnd); 91 } 92 } 93 } 94 } 95 }