springmvc关闭url后缀匹配访问

现象:

例如:当我们想下载附件时访问url:/affix/xxx/download时便可访问。接口如下图:

	@RequestMapping(value = {"/affix/{encryptionId}/download"}, method = {RequestMethod.GET})
	public void download(@PathVariable String encryptionId, HttpServletRequest request, HttpServletResponse response) {
        //xxx

	}

但是当我们访问/affix/xxx/download增加.xxx的后缀名时也能下载。例如访问:/affix/xxx/download.exe

这是为啥?为什么会匹配到这个Handler?

 

原因:

通过钻研源码,发现了springmvc在获取处理器映射器阶段会根据用户请求的url查找对应的HandlerMapping,

在查找时会通过PatternsRequestCondition类匹配url的后缀(默认时开启的),url中如果有小数点,它便会匹配成功这个Handler(过程是用/affix/xxx/download.* 去匹配请求 /affix/xxx/download.exe

SpringMVC工作流程参考:https://blog.csdn.net/sumengnan/article/details/105381225

 

解决办法:

错误做法:

一开始我是自定义类继承WebMvcConfigurationSupport类,重写requestMappingHandlerMapping方法,把UseSuffixPatternMatch值设置为false可以了。

@Configuration
 public class MyWebMvcConfiguration extends WebMvcConfigurationSupport {

      @Override
      public RequestMappingHandlerMapping requestMappingHandlerMapping() {
            RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
            handlerMapping.setUseSuffixPatternMatch(false);//设置禁止url后缀匹配
            return handlerMapping;
      }
 }

踩坑:

在我们环境是springboot时,并且采用了继承WebMvcConfigrutionSupport类的方式。

后来发现因为springboot自动配置类WebMvcAutoConfiguration有@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)注解,springboot就不会帮我们自动配置了,我们还需要手动配置各个组件,如ResourceHandlers 否则会导致访问静态资源的时候出现404

 

正确做法:

应该继承WebMvcConfigurerAdapter(虽然过时了也能用),或是实现WebMvcConfigurer接口都一样

@Configuration
public class JswWebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);//设置禁止url后缀匹配
    }
}

 

相关:

类似的还有useTrailingSlashMatch属性,用于匹配尾部“/”

这就是当你访问url时尾部多加了“/”也能正常访问的原因

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`addurlpatterns`不是Spring MVC框架中的函数,而是Django Web框架中的函数。在Spring MVC中,URL与控制器的映射是通过`@RequestMapping`注解来实现的,而非使用`addurlpatterns`函数。 在Spring MVC中,我们可以使用`@RequestMapping`注解来定义URL与控制器方法之间的映射关系。例如: ``` @Controller @RequestMapping("/users") public class UserController { @RequestMapping(value = "/{userId}", method = RequestMethod.GET) public String getUser(@PathVariable String userId, Model model) { User user = userRepository.findById(userId); model.addAttribute("user", user); return "user"; } @RequestMapping(value = "/new", method = RequestMethod.GET) public String newUser(Model model) { model.addAttribute("user", new User()); return "userForm"; } @RequestMapping(value = "/new", method = RequestMethod.POST) public String createUser(@ModelAttribute User user) { userRepository.save(user); return "redirect:/users/" + user.getId(); } } ``` 在上述代码中,`@RequestMapping`注解指定了`/users`前缀,即所有的请求都以`/users`开头,而`value="/{userId}"`则表示该方法处理`/users/{userId}`的请求。同时,`@RequestMapping`注解还可以指定HTTP请求方法,例如`method = RequestMethod.GET`表示该方法只处理GET请求。 需要注意的是,Spring MVC中并没有内置的非匹配功能。如果需要在URL配置中排除某些URL,可以通过在控制器方法中进行判断来实现。例如: ``` @Controller public class UserController { @RequestMapping(value = "/admin/**", method = RequestMethod.GET) public String adminPage(HttpServletRequest request) { String uri = request.getRequestURI(); if (uri.startsWith("/admin/")) { return "admin"; } else { return "pageNotFound"; } } } ``` 在上述代码中,我们使用了`HttpServletRequest`对象来获取当前请求的URI,然后判断其是否以`/admin/`开头,如果是,则返回`admin`页面,否则返回`pageNotFound`页面。这样就可以实现类似于Django中的非匹配功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值