Spring/Rest @PathVariable 字符编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4470787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:15:27  来源:igfitidea点击:

Spring/Rest @PathVariable character encoding

springrestcharacter-encodinguri

提问by Julian Reschke

In the environment I'm using (Tomcat 6), percent sequences in path segments apparently are decoded using ISO-8859-1 when being mapped to a @PathVariable.

在我使用的环境(Tomcat 6)中,路径段中的百分比序列在映射到 @PathVariable 时显然是使用 ISO-8859-1 解码的。

I'd like that to be UTF-8.

我希望它是 UTF-8。

I already configured Tomcat to use UTF-8 (using the URIEncoding attribute in server.xml).

我已经将 Tomcat 配置为使用 UTF-8(使用 server.xml 中的 URIEncoding 属性)。

Is Spring/Rest doing the decoding on its own? If yes, where can I override the default encoding?

Spring/Rest 是否自己进行解码?如果是,我在哪里可以覆盖默认编码?

Additional information; here's my test code:

附加信息; 这是我的测试代码:

@RequestMapping( value = "/enc/{foo}", method = RequestMethod.GET )
public HttpEntity<String> enc( @PathVariable( "foo" ) String foo, HttpServletRequest req )
{
  String resp;

  resp = "      path variable foo: " + foo + "\n" + 
         "      req.getPathInfo(): " + req.getPathInfo() + "\n" +
         "req.getPathTranslated(): " + req.getPathTranslated() + "\n" + 
         "    req.getRequestURI(): " + req.getRequestURI() + "\n" + 
         "   req.getContextPath(): " + req.getContextPath() + "\n";

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType( new MediaType( "text", "plain", Charset.forName( "UTF-8" ) ) );
  return new HttpEntity<String>( resp, headers );
}

If I do an HTTP GET request with the following URI path:

如果我使用以下 URI 路径执行 HTTP GET 请求:

/TEST/enc/%c2%a3%20and%20%e2%82%ac%20rates

which is the UTF-8 encoded then percent-encoded form of

这是 UTF-8 编码然后百分比编码的形式

/TEST/enc/£ and  rates

the output that I get is:

我得到的输出是:

      path variable foo: ?£ and a? rates
      req.getPathInfo(): /enc/£ and  rates
req.getPathTranslated(): C:\Users\jre\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\TEST\enc\£ and  rates
    req.getRequestURI(): /TEST/enc/%C2%A3%20and%20%E2%82%AC%20rates
   req.getContextPath(): /TEST

which to me shows that Tomcat (after setting the URIEncoding attribute) does the right thing (see getPathInfo()), but the path variable is decoded still in ISO-8859-1.

对我来说,这表明 Tomcat(在设置 URIEncoding 属性之后)做了正确的事情(参见 getPathInfo()),但路径变量仍然在 ISO-8859-1 中解码。

And the answer is:

答案是

Spring/Rest apparently uses the request encoding, which is a very strange thing to do, as this is about the body, not the URI. Sigh.

Spring/Rest 显然使用请求编码,这是一件非常奇怪的事情,因为这是关于body,而不是 URI。叹。

Adding this:

添加这个:

<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

fixed the problem. It really should be simpler.

解决了这个问题。它真的应该更简单。

And actually, it's worse:

实际上,情况更糟:

If the method indeed hasa request body, and that one isn't encoded in UTF-8, the additional forceEncoding parameter is needed. This seems to work, but I'm concerned it will cause more problems later on.

如果该方法确实一个请求主体,并且该主体不是以 UTF-8 编码的,则需要额外的 forceEncoding 参数。这似乎有效,但我担心以后会引起更多问题。

Another approach

另一种方法

In the meantime, I found out that it's possible to disable the decoding, my specifying

与此同时,我发现可以禁用解码,我指定

<property name="urlDecode" value="false"/>

...in which case the recipient can to the right thing; but of course this will make lots of other things harder.

...在这种情况下,收件人可以做正确的事情;但这当然会使许多其他事情变得更加困难。

回答by Hurda

I thing that you need add filter to web.xml

我需要向 web.xml 添加过滤器

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

回答by 11101101b

The path variable is still decoded in ISO-8859-1 for me, even with the Character Encoding Filter. Here is what I had to do to get around this. Please let me know if you have any other ideas!

对我来说,路径变量仍然在 ISO-8859-1 中解码,即使使用字符编码过滤器也是如此。这是我必须做的来解决这个问题。如果您有任何其他想法,请告诉我!

To see the actual UTF-8 decoded characters on the server, you can just do this and take a look at the value (you need to add "HttpServletRequest httpServletRequest" to your controller parameters):

要查看服务器上实际的 UTF-8 解码字符,您只需执行此操作并查看值(您需要将“HttpServletRequest httpServletRequest”添加到您的控制器参数中):

String requestURI = httpServletRequest.getRequestURI();
String decodedURI = URLDecoder.decode(requestURI, "UTF-8");

I can then do whatever I want (like get the parameter manually from the decoded URI), now that I have the right decoded data on the server.

然后我可以做任何我想做的事情(比如从解码的 URI 手动获取参数),现在我在服务器上有正确的解码数据。

回答by Avseiytsev Dmitriy

Try to configure connector on Tomcat in server.xml. Add useBodyEncodingForURI="true"or URIEncoding="UTF-8"to your Connector tag. For example:

尝试在 server.xml 中配置 Tomcat 上的连接器。添加useBodyEncodingForURI="true"URIEncoding="UTF-8"到您的连接器标签。例如:

    <Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           useBodyEncodingForURI="true"
           redirectPort="8443" />

回答by Olaf Klischat

But doesn't it suck that you have to mess with the Tomcat configuration (URIEncoding) at all to make this work? If the servlet API provided a way to obtain the path and request parameters in their undecoded representation, the application (or Spring) could deal with the decoding entirely on its own. And apparently, HttpServletRequest#getPathInfoand HttpServletRequest#getQueryStringwould even provide this, but for the latter this would mean that Spring would have to parse and decode the query string itself and not rely on HttpServletRequest#getParameterand friends. Apparently they don't do this, which means you can't have @RequestParamor @PathVariablecapture anything other than us-ascii strings safely without relying on the servlet container's configuration.

但是,您必须完全弄乱 Tomcat 配置 (URIEncoding) 才能使其正常工作,这不是很糟糕吗?如果 servlet API 提供了一种获取路径和请求参数未解码表示的方法,则应用程序(或 Spring)可以完全自行处理解码。而且很显然,HttpServletRequest#getPathInfoHttpServletRequest#getQueryString甚至会提供这一点,但对于后者,这将意味着春天将不得不解析和解码查询字符串本身,而不是依靠HttpServletRequest#getParameter和朋友。显然他们不这样做,这意味着您不能在不依赖 servlet 容器的配置的情况下安全地拥有@RequestParam@PathVariable捕获 us-ascii 字符串以外的任何内容。