Spring Boot MVC错误编码的POST请求 [英] spring boot MVC wrong encoded POST request

查看:86
本文介绍了Spring Boot MVC错误编码的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使请求编码正常工作。为了使编码正常工作,我在Spring Security中添加了过滤器:

I can not make request encoding to work correctly. For encoding to work, i added filter to spring security:

@Bean
public CharacterEncodingFilter characterEncodingFilter() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    return filter;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.addFilterBefore(characterEncodingFilter(), CsrfFilter.class);
...
}

将meta添加到我的页面:

Add meta to my pages:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
...

在tomcat 8中添加UriEncon

Add UriEnconding to tomcat 8:

<Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
   connectionTimeout="20000"
   redirectPort="8443" />

但是它没有响应。当我发送请求并调试它时,结果是错误的。例如,当我发送بای作为请求表的一部分时,我收到باÛ的结果。我缺少配置的任何部分吗?

But it is not responding to none of them. When i send a request and debug it, the result is wrong. For example when i send بای as a part of request form, i recieve Ø¨Ø§Û as result. Is there any part of config i am missing?

在发送有效负载ajax请求(带有其他客户端和程序的其他方法)时,它可以正常工作,但是表单数据没有运气。我的控制器如下所示:

When sending payload ajax request (with rest client and ofcource to another method) it workes correctly, but no luck with form data. My controller looks like this:

@RequestMapping(value = "/test1")
@ResponseBody
public String test1(@RequestBody String req) {
    return req;
}

@RequestMapping(value = "/test2")
@ResponseBody
public String test2(@RequestParam("search") String req) {
    return req;
}

不同尝试:


  1. 对于带有 Content-Type = application / json 的test1,它正确地接收了该参数。

  2. 使用 Content-Type = application / x-www-form-urlencoded 和get方法的test2,一切都很好。

  3. 用于test2使用 Content-Type = application / x-www-form-urlencoded 和POST方法,它将返回错误的编码值。

  1. for test1 with Content-Type=application/json it correctly recieves the parameter.
  2. for test2 with Content-Type=application/x-www-form-urlencoded and get method, again everything works great.
  3. for test2 with Content-Type=application/x-www-form-urlencoded and POST method, it returns wrong encoded values.

似乎问题仅在于POST方法。有什么建议吗?

It seems the problem is with POST method only. any suggestions?

推荐答案

深入研究许多过滤器链并在其中移动了字符编码后,终于找到了问题。

After digging into lots of filter chains and moving character encoding in them, finally found the problem. Another filter was changing encoding before Character encoding kick in.

我只是从spring security手中得到了过滤器,并将其作为bean手动添加,为我做过滤器:

I just get filter from spring security's hand and add it manually as a bean to do the filtering for me:

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    registrationBean.setFilter(characterEncodingFilter);
    return registrationBean;
}

2天或搜索并尝试,只是一个简单的答案。找到答案的困难方式

2 days or searching and trying and just a simple answer. What a hard way to find an answer

---更新---

如果您使用的是Spring Boot 1.2+,则完全不需要CharacterEncoding。默认情况下,它处于理想位置,字符编码等于utf8。因此,下次,只要设置属性(如有)即可! 要设置的属性

If you are using spring boot 1.2+, there is no need for CharacterEncoding at all. it is in its perfect position by default with character encoding equal to utf8. So next time, just set attributes if there are any! the properties to set are:

# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8 # the encoding of HTTP requests/responses
spring.http.encoding.enabled=true # enable http encoding support
spring.http.encoding.force=true # force the configured encoding

---更新2 ---

Tomcat Wiki :在 Tomcat 8 中从8.0.0(具体来说是8.0.0-RC3)开始,元素上的 URIEncoding 属性的默认值取决于严格的servlet遵从性设置。 URIEncoding的默认值(严格符合性已关闭)现在为 UTF-8 。如果启用了严格的servlet遵从性,则默认值为 ISO-8859-1

From Tomcat wiki: In Tomcat 8 starting with 8.0.0 (8.0.0-RC3, to be specific), the default value of URIEncoding attribute on the element depends on "strict servlet compliance" setting. The default value (strict compliance is off) of URIEncoding is now UTF-8. If "strict servlet compliance" is enabled, the default value is ISO-8859-1.

,则无需在Tomcat Config neigter中设置UriEncoding。

so generally saying, there is no need to set the UriEncoding in Tomcat Config neigter.

这篇关于Spring Boot MVC错误编码的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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