Jetty Utf8Appendable $ NotUtf8Exception on ISO-8859 Request with Spring [英] Jetty Utf8Appendable$NotUtf8Exception on ISO-8859 Request with Spring

查看:416
本文介绍了Jetty Utf8Appendable $ NotUtf8Exception on ISO-8859 Request with Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

远程服务使用ISO-8859-15编码的请求来呼叫Jetty Server。此特殊请求映射到Spring控制器。 Jetty无法以正确的方式对请求进行编码,并显示以下异常:

A Remote Service calls our Jetty Server with a Request encoded in ISO-8859-15. This special request is mapped on a Spring Controller. Jetty is not able to encode the request in right manner and shows the following exception:

exception=org.eclipse.jetty.util.Utf8Appendable$NotUtf8Exception: Not valid UTF8! byte F6 in state 3}
org.eclipse.jetty.util.Utf8Appendable$NotUtf8Exception: Not valid UTF8! byte F6 in state 3
    at org.eclipse.jetty.util.Utf8Appendable.appendByte(Utf8Appendable.java:168) ~[na:na]
    at org.eclipse.jetty.util.Utf8Appendable.append(Utf8Appendable.java:93) ~[na:na]
    at org.eclipse.jetty.util.UrlEncoded.decodeUtf8To(UrlEncoded.java:506) ~[na:na]
    at org.eclipse.jetty.util.UrlEncoded.decodeTo(UrlEncoded.java:554) ~[na:na]
    at org.eclipse.jetty.server.Request.extractParameters(Request.java:285) ~[na:na]
    at org.eclipse.jetty.server.Request.getParameter(Request.java:695) ~[na:na]
    ....






解决方案

在Spring中,即使整个应用程序使用UTF-8,也可以通过CharacterEncodingFilter强制执行请求的编码。异常应该消失。

In Spring it's possible to force an encoding of the request through a CharacterEncodingFilter even if the whole application speaks UTF-8. The Exception should disappear.

<filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>ISO-8859-15</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/app/specialRequest.do</url-pattern>
</filter-mapping>

如果这不适合你


  • 找出远程系统编码

  • 启动Wireshark通过ip.src == xxx.xxx.xxx.xxx过滤器
  • $分析传入包b $ b
  • 在请求正文中搜索特殊字符(将十六进制值重新计算为二进制,并尝试使用多个常用编码,以查找与异常相匹配的编码)

  • 通过Jetty的start.ini设置编码ie。使用以下参数

  • find out the remote system encoding
  • start Wireshark to analyze incoming package through ip.src == xxx.xxx.xxx.xxx filter
  • search the requests body for special characters (recalculate the hex value to binary and try several frequently used encodings to find exactly the one who is matched the exception)
  • set encoding through Jetty's start.ini ie. with the following parameters

Dorg.eclipse.jetty.util.URI.charset = ISO-8859-15

Dorg.eclipse.jetty.util.URI.charset=ISO-8859-15

Dorg.eclipse.jetty.util.UrlEncoding.charset = ISO-8859-15

Dorg.eclipse.jetty.util.UrlEncoding.charset=ISO-8859-15

否则给我留言如果你有更多的问题。

Otherwise drop me a message if you have more questions.

推荐答案

看起来客户端正在发送应编码为UTF8的文本,但不是

It looks like the client is sending text that should be encoded as UTF8, but isn't encoding it.

为了正确诊断此问题,您需要了解UTF8(您可能会做,我不知道)

In order to properly diagnose this issue you'll need to understand UTF8 (which you might do, I don't know)

在UTF8中,使用编码为127(0x7F)或更小的 - ie 仅使用最低7位的任何字符都包含在流(无特殊编码)。但是,任何大于127(即至少比第七位高一个位置1)被特别编码。

In UTF8 any character with an encoding of 127 (0x7F) or less - i.e. only the lowest 7 bit are used - is included in the stream as is (no special encoding). But anything greater than 127 (i.e. at least one bit higher than the 7th is set), is specially encoded.

0xF6 大于 0x7F ,所以如果客户端想要发送该字符,它应该对其进行编码。

0xF6 is greater than 0x7F so if a client wants to send that character, it should encoded it.

0xF6 二进制是 11110110 ,其中UTF8应该是 11000011 10110110 C3 B6

0xF6 in binary is 11110110, which in UTF8 should be 11000011 10110110 (C3 B6)

所以如果客户端想要发送0xF6的ISO8859-1字符,那么应该发送0xC3 0xB6的UTF8字节序列。

So, if the client wants to send the ISO8859-1 character of 0xF6, then it should be sending the UTF8 byte sequence of 0xC3 0xB6.

你真的需要找出什么客户端想要发送,数据是什么字符集/编码,以及为什么在发送数据之前不将其转换为有效的UTF8。

You really need to work out what the client wants to be sending, what charset/encoding that data is in, and why it's not converting it to valid UTF8 before it sends it.

(状态3,是与Jetty的内部表进行UTF8解码,对于诊断这个问题并不是非常有帮助,只要您找到客户端,它将会派上用场,而且客户端正在做正确的事情,你怀疑Jetty的UTF8解码是错误的)

这篇关于Jetty Utf8Appendable $ NotUtf8Exception on ISO-8859 Request with Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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