spring boot 中获取所有 RequestMapping 的 URL 路径列表集
spring boot 项目在做 URL 权限控制的时候需要获得全部的 URL,一个一个去 controller 中找费时费力,有的权限点的命名和 URL 有一定的对应关系。如果能用程序获得全部 URL,将会省去很多事。在项目中添加如下 Controller,请求 /getAllUrl,即可看到项目所有的 URL。当然也可以根据项目将 URL 写入数据库或写入配置文件。
-
-
WebApplicationContext applicationContext;
-
-
-
public Object 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);
-
// }
-
// }
-
-
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
-
for (Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
-
Map<String, String> map1 = new HashMap<String, String>();
-
RequestMappingInfo info = m.getKey();
-
HandlerMethod method = m.getValue();
-
PatternsRequestCondition p = info.getPatternsCondition();
-
for (String url : p.getPatterns()) {
-
map1.put("url", url);
-
}
-
map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
-
map1.put("method", method.getMethod().getName()); // 方法名
-
RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
-
for (RequestMethod requestMethod : methodsCondition.getMethods()) {
-
map1.put("type", requestMethod.toString());
-
}
-
-
list.add(map1);
-
}
-
-
JSONArray jsonArray = JSONArray.fromObject(list);
-
-
return jsonArray;
-
}
参考 http://jayung.iteye.com/blog/2240864,
https://blog.csdn.net/Clark_Lu/article/details/78670411?locationNum=7&fps=1
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.method.HandlerMethod;
- import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
- import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
- import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
- import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
- @Controller
- public class MappingController {
- @Autowired
- private RequestMappingHandlerMapping requestMappingHandlerMapping;
- @RequestMapping(value = "/mappings")
- public String list(Model model) {
- List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();
- Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
- for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
- HashMap<String, String> hashMap = new HashMap<String, String>();
- RequestMappingInfo info = m.getKey();
- HandlerMethod method = m.getValue();
- PatternsRequestCondition p = info.getPatternsCondition();
- for (String url : p.getPatterns()) {
- hashMap.put("url", url);
- }
- hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
- hashMap.put("method", method.getMethod().getName()); // 方法名
- RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
- String type = methodsCondition.toString();
- if (type != null && type.startsWith("[") && type.endsWith("]")) {
- type = type.substring(1, type.length() - 1);
- hashMap.put("type", type); // 方法名
- }
- urlList.add(hashMap);
- }
- model.addAttribute("list", urlList);
- return "/console/system/mappingList";
- }
- }
然后再在页面上遍历 list 即可
- <table class="tableList" >
- <tr>
- <th> 类名 </th>
- <th> 方法名 </th>
- <th>URL</th>
- <th> 类型 </th>
- <tr>
- <c:forEach items="${list}" var="mvc" varStatus="status">
- <tr id="${status.index}">
- <td>${mvc.className}</td>
- <td>${mvc.method}</td>
- <td>
- <c:choose>
- <c:when test="${!fn:contains(mvc.url,'}') and (mvc.type=='GET' or mvc.type=='')}">
- <a href="${ctx}${mvc.url}" target="_blank">${mvc.url}</a>
- </c:when>
- <c:otherwise>${mvc.url}</c:otherwise>
- </c:choose>
- </td>
- <td>${mvc.type}</td>
- </tr>
- </c:forEach>
- </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;
}
}
摘抄自网络,便于检索查找。