How to change spring request mapping to disallow url pattern with suffix

我的未来我决定 提交于 2021-02-08 04:21:36

问题


I am using Spring MVC 3.2 and deploying in Apache Tomcat 1.7x. My login url is /web/login but using url /web/login.abc where abc can be any text including space.

In both cases it is returning the same resource which I will like to avoid and return HTTP code 404.

Tried adding the below in web.xml but it did not help

`<beans:bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <beans:property name="useDefaultSuffixPattern" value="false" />
</beans:bean>`

回答1:


This config depends on the version you're using which you've omitted in your question. Since Spring 4.0.3 the suffix properties are set on the PathMatchConfigurer class. As per Spring doc the config should be under mvc:annotation-driven, e.g.

  <mvc:annotation-driven>
    <mvc:path-matching suffix-pattern="false" />
  </mvc:annotation-driven>

as explained in the docs

Whether to use suffix pattern match (".*") when matching patterns to requests. If enabled a method mapped to "/users" also matches to "/users.*". The default value is true.

For Spring 3.2 it should be

  <beans:bean id="handlerMapping"
 class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
      <beans:property name="useSuffixPatternMatch" value="false"/>
  </beans:bean>`

Also, if you're using the mvc:annotation-driven element in your config, take a note of Biju's answer from this question How do I restrict route extensions in @RequestMapping paths for Spring MVC controllers?



来源:https://stackoverflow.com/questions/30610607/how-to-change-spring-request-mapping-to-disallow-url-pattern-with-suffix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!