springboot项目中获取所有requestmapping的url路径

目前项目上线之后因为他人恶意请求项目导致项目内存溢出报如下错误

2019/06/26-02:42 [http-bio-19000-exec-273] ERROR org.springframework.boot.web.support.ErrorPageFilter- Forwarding to error page from request [error/n/n/n] due to exception [Java heap space]
java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3332)
	at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)
	at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)
	at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:421)
	at java.lang.StringBuffer.append(StringBuffer.java:272)
	at java.text.MessageFormat.subformat(MessageFormat.java:1335)
	at java.text.MessageFormat.format(MessageFormat.java:821)
	at org.apache.naming.StringManager.getString(StringManager.java:142)
	at org.apache.naming.resources.BaseDirContext.getAttributes(BaseDirContext.java:1179)
	at org.apache.naming.resources.BaseDirContext.getAttributes(BaseDirContext.java:1110)
	at org.apache.naming.resources.ProxyDirContext.cacheLoad(ProxyDirContext.java:1611)
	at org.apache.naming.resources.ProxyDirContext.cacheLookup(ProxyDirContext.java:1534)
	at org.apache.naming.resources.ProxyDirContext.lookup(ProxyDirContext.java:295)
	at org.apache.tomcat.util.http.mapper.Mapper.internalMapWrapper(Mapper.java:1061)
	at org.apache.tomcat.util.http.mapper.Mapper.map(Mapper.java:762)
	at org.apache.catalina.core.ApplicationContext.getRequestDispatcher(ApplicationContext.java:522)
	at org.apache.catalina.core.ApplicationContextFacade.getRequestDispatcher(ApplicationContextFacade.java:222)
	at org.apache.catalina.core.ApplicationHttpRequest.getRequestDispatcher(ApplicationHttpRequest.java:338)
	at org.springframework.web.servlet.view.InternalResourceView.getRequestDispatcher(InternalResourceView.java:223)
	at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:148)
	at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1271)
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

所以为了防止他人再次恶意请求导致项目线上内存溢出决定判断所有的url请求是否存在requestmapping中

	private static List urls = new ArrayList();
	@Autowired
	WebApplicationContext applicationContext;
	public List urls() {
		RequestMappingHandlerMapping mapping = applicationContext
				.getBean(RequestMappingHandlerMapping.class);
		// 获取url与类和方法的对应信息
		Map map = mapping
				.getHandlerMethods();
		for (RequestMappingInfo info : map.keySet()) {
			// 获取url的Set集合,一个方法可能对应多个url
			Set patterns = info.getPatternsCondition().getPatterns();
			for (String url : patterns) {
				//把结果存入静态变量中程序运行一次次方法之后就不用再次请求次方法
				urls.add(url);
			}
		}
		return urls;
	}```
然后自定义判断请求中是合法,在控制器中决定do something;

你可能感兴趣的:(G工作中的问题)