具有百分比编码字符的Spring MVC uri映射 [英] Spring MVC uri mapping with percent encoded characters

查看:146
本文介绍了具有百分比编码字符的Spring MVC uri映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个spring mvc应用程序在Tomcat 7上运行,其中http和ajp连接器配置了URIEncoding =UTF-8。
我的REST控制器片段:

  @Controller 
@RequestMapping()
public ClientRestApi类{
private final String API_PREFIX =/api/1.0/client;
...
@RequestMapping(value = API_PREFIX +/ {clientId},method = RequestMethod.GET)
@ResponseBody
public ClientDetails get(@PathVariable String clientId, HttpServletRequest request){
log.info(Client API GET [+ clientId +] |+ request.getRequestURI());
...
}
}




  • 当我转到: http://www.example.pl/api/1.0/client/abc 我得到 abc 客户端页面 - 正确

  • 当我转到: http://www.example.pl/%07api/1.0/client/abc 我得到 abc 客户端页面 - 错误

  • 当我去: http ://www.example.pl/%0bapi/1.0/client/abc 我得到 abc 客户页面 - 错误

  • 当我去: http://www.example.pl/ap%0bi/1.0/client/abc 我得到http 404 - 正确



在应用程序日志中,我可以看到(前3个请求):

  ClientRestApi  - 客户端API GET [abc] | /api/1.0/client/abc 
ClientRestApi - 客户端API GET [abc] | /%07api/1.0/client/abc
ClientRestApi - 客户端API GET [abc] | /%0bapi/1.0/client/abc

我的问题是为什么我的错误<强>示例是错误的? 为什么它们不是http 404?



在应用程序中的web.xml文件中,我具有使用UTF-8编码的CharacterEncodingFilter过滤器。我的应用程序中从未有任何编码错误的问题。



编辑:
从扩展日志中,请求 http://www.example.pl/%0bapi/1.0/client/abc 给出:

  DEBUG RequestMappingHandlerMapping  - 查找路径的处理方法/^Kapi/1.0/client/abc 
TRACE RequestMappingHandlerMapping - 找到1个匹配的映射[/^Kapi/1.0/client/abc]:[ {[/api/1.0/client/ {
clientId}],methods = [GET],params = [],headers = [],consumption = [],produce = [],custom = []}]
DEBUG RequestMappingHandlerMapping - 返回处理程序方法[public ClientDetails ... ClientRestApi.get(java.lang.String,javax.servlet.http.HttpServletRequest)]


解决方案

AntPathMatcher对路径进行标记,默认情况下修剪所有段(请参阅String.trim的Javadoc)。可以控制这种行为。您可以使用setTrimTokens(false)配置具有AntPathMatcher的RequestMappingHandlerMapping。


I have a spring mvc application that runs on Tomcat 7 with http and ajp connectors configured with URIEncoding="UTF-8". My REST controller fragments:

@Controller
@RequestMapping("")
public class ClientRestApi {
    private final String API_PREFIX = "/api/1.0/client";
    ...
    @RequestMapping(value = API_PREFIX + "/{clientId}", method = RequestMethod.GET)
    @ResponseBody
    public ClientDetails get(@PathVariable String clientId, HttpServletRequest request) {
        log.info("Client API GET [" + clientId + "] | " + request.getRequestURI());
        ...
    }
}

  • When i go to: http://www.example.pl/api/1.0/client/abc i get abc client page - correct
  • When i go to: http://www.example.pl/%07api/1.0/client/abc i get abc client page - wrong
  • When i go to: http://www.example.pl/%0bapi/1.0/client/abc i get abc client page - wrong
  • When i go to: http://www.example.pl/ap%0bi/1.0/client/abc i get http 404 - correct

In application log i can see (for first 3 requests):

ClientRestApi - Client API GET [abc] | /api/1.0/client/abc
ClientRestApi - Client API GET [abc] | /%07api/1.0/client/abc
ClientRestApi - Client API GET [abc] | /%0bapi/1.0/client/abc

My question is why does my wrong examples are wrong? Why they are not http 404?

In web.xml file in application i have CharacterEncodingFilter filter with UTF-8 encoding. I have never had any issues with wrong encoding in my application.

Edit: From extended logs, request http://www.example.pl/%0bapi/1.0/client/abc gives:

DEBUG RequestMappingHandlerMapping - Looking up handler method for path /^Kapi/1.0/client/abc
TRACE RequestMappingHandlerMapping - Found 1 matching mapping(s) for [/^Kapi/1.0/client/abc] : [{[/api/1.0/client/{
clientId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}]
DEBUG RequestMappingHandlerMapping - Returning handler method [public ClientDetails ...ClientRestApi.get(java.lang.String,javax.servlet.http.HttpServletRequest)]

解决方案

AntPathMatcher tokenizes the path and by default trims all segments (see the Javadoc for String.trim). This behavior can be controlled. For you can configure the RequestMappingHandlerMapping with an AntPathMatcher with setTrimTokens(false).

这篇关于具有百分比编码字符的Spring MVC uri映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆