spring boot 中获取所有 RequestMapping 的 URL 路径列表集

spring boot 项目在做 URL 权限控制的时候需要获得全部的 URL,一个一个去 controller 中找费时费力,有的权限点的命名和 URL 有一定的对应关系。如果能用程序获得全部 URL,将会省去很多事。在项目中添加如下 Controller,请求 /getAllUrl,即可看到项目所有的 URL。当然也可以根据项目将 URL 写入数据库或写入配置文件。

 
  1. @Autowired
  2. WebApplicationContext applicationContext;
  3.  
  4. @RequestMapping(value = "v1/getAllUrl", method = RequestMethod.POST)
  5. public Object getAllUrl() {
  6. RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
  7. // 获取url与类和方法的对应信息
  8. Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
  9.  
  10. // List<String> urlList = new ArrayList<>();
  11. // for (RequestMappingInfo info : map.keySet()) {
  12. // // 获取url的Set集合,一个方法可能对应多个url
  13. // Set<String> patterns = info.getPatternsCondition().getPatterns();
  14. //
  15. // for (String url : patterns) {
  16. // urlList.add(url);
  17. // }
  18. // }
  19.  
  20. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  21. for (Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
  22. Map<String, String> map1 = new HashMap<String, String>();
  23. RequestMappingInfo info = m.getKey();
  24. HandlerMethod method = m.getValue();
  25. PatternsRequestCondition p = info.getPatternsCondition();
  26. for (String url : p.getPatterns()) {
  27. map1.put("url", url);
  28. }
  29. map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
  30. map1.put("method", method.getMethod().getName()); // 方法名
  31. RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
  32. for (RequestMethod requestMethod : methodsCondition.getMethods()) {
  33. map1.put("type", requestMethod.toString());
  34. }
  35.  
  36. list.add(map1);
  37. }
  38.  
  39. JSONArray jsonArray = JSONArray.fromObject(list);
  40.  
  41. return jsonArray;
  42. }
 

参考 http://jayung.iteye.com/blog/2240864
https://blog.csdn.net/Clark_Lu/article/details/78670411?locationNum=7&fps=1

  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.ui.Model;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.method.HandlerMethod;  
  11. import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;  
  12. import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;  
  13. import org.springframework.web.servlet.mvc.method.RequestMappingInfo;  
  14. import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;  
  15.   
  16. @Controller  
  17. public class MappingController {  
  18.   
  19.     @Autowired  
  20.     private RequestMappingHandlerMapping requestMappingHandlerMapping;  
  21.   
  22.     @RequestMapping(value = "/mappings")  
  23.     public String list(Model model) {  
  24.         List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();  
  25.   
  26.         Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();  
  27.         for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {  
  28.             HashMap<String, String> hashMap = new HashMap<String, String>();  
  29.             RequestMappingInfo info = m.getKey();  
  30.             HandlerMethod method = m.getValue();  
  31.             PatternsRequestCondition p = info.getPatternsCondition();  
  32.             for (String url : p.getPatterns()) {  
  33.                 hashMap.put("url", url);  
  34.             }  
  35.             hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名  
  36.             hashMap.put("method", method.getMethod().getName()); // 方法名  
  37.             RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();  
  38.             String type = methodsCondition.toString();  
  39.             if (type != null && type.startsWith("[") && type.endsWith("]")) {  
  40.                 type = type.substring(1, type.length() - 1);  
  41.                 hashMap.put("type", type); // 方法名  
  42.             }  
  43.             urlList.add(hashMap);  
  44.         }  
  45.         model.addAttribute("list", urlList);  
  46.         return "/console/system/mappingList";  
  47.     }  
  48.   
  49. }  

 然后再在页面上遍历 list 即可

Java 代码  收藏代码
  1. <table class="tableList" >  
  2. <tr>  
  3.         <th> 类名 </th>  
  4.         <th> 方法名 </th>  
  5.         <th>URL</th>  
  6.         <th> 类型 </th>  
  7. <tr>  
  8.     <c:forEach items="${list}" var="mvc" varStatus="status">  
  9.     <tr id="${status.index}">  
  10.         <td>${mvc.className}</td>  
  11.         <td>${mvc.method}</td>  
  12.         <td>  
  13.             <c:choose>  
  14.             <c:when test="${!fn:contains(mvc.url,'}') and (mvc.type=='GET' or mvc.type=='')}">  
  15.                 <a href="${ctx}${mvc.url}" target="_blank">${mvc.url}</a>  
  16.             </c:when>  
  17.             <c:otherwise>${mvc.url}</c:otherwise>  
  18.             </c:choose>  
  19.         </td>  
  20.         <td>${mvc.type}</td>  
  21.     </tr>  
  22.     </c:forEach>  
  23. </table>  

spring boot 项目在做 URL 权限控制的时候需要获得全部的 URL,一个一个去 controller 中找费时费力。而且有的权限点的命名和 URL 有一定的对应关系。如果能用程序获得全部 URL,将会省去很多事。下面就介绍一种获取 URL 的方法。在项目中添加如下 Controller,请求 /getAllUrl,即可看到项目所有的 URL。当然也可以根据项目将 URL 写入数据库或写入配置文件。


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/*")
public class UrlController {

    @Autowired
    WebApplicationContext applicationContext;
    @GetMapping("/getAllUrl")
    @ResponseBody
    public List<String> getAllUrl(){
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        //获取url与类和方法的对应信息
        Map<RequestMappingInfo,HandlerMethod> map = mapping.getHandlerMethods();
        List<String> urlList = new ArrayList<>();
        for (RequestMappingInfo info : map.keySet()){
            //获取url的Set集合,一个方法可能对应多个url
            Set<String> patterns = info.getPatternsCondition().getPatterns();
            for (String url : patterns){
                urlList.add(url);
            }
        }
        return urlList;
    }

}

 

 

 

 

posted @ 2024-06-26 14:46  CharyGao  阅读(48)  评论(0编辑  收藏  举报