Spring-Cloud Zuul在转发的多部分请求文件名中破坏了UTF-8符号 [英] Spring-Cloud Zuul breaks UTF-8 symbols in forwarded multipart request filename

查看:263
本文介绍了Spring-Cloud Zuul在转发的多部分请求文件名中破坏了UTF-8符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说是第一次,所以请耐心等待我的第一个问题.

this is first time for me on SO, so please be patient for my first question.

我认为我遇到某种配置问题,但是经过一天的实验,我陷入了困境.我们的应用程序基于Spring-Cloud [Brixton版本].我们的配置如下:Portal(服务于基于角度的Web-ui的Web应用程序),其中的zuul代理配置了到我们的网关服务的单个路由,如下所示:

I think i have some kind of configuration problem, but after a day of experiments i'm stuck. Our application is based on Spring-Cloud [Brixton release]. We have configuration like this: Portal (web application serving angular-based web-ui), which has zuul proxy with single route configured to our gateway service, like so:

zuul:
   ignoredServices: '*'
   prefix: /api
   routes:
       api-proxy:
          path: /**
          serviceId: api-gateway

已配置了另一个Zuul,并将请求中继到内部业务逻辑服务:

which has another Zuul configured and relays requests to inner bussiness logic services:

zuul:
  ignoredServices: '*'
  routes:
     service1:
       path: /service1/**
       serviceId: service1
     service2:
       path: /service2/**
       serviceId: service2

所有此配置均正常运行. 我现在面临的问题是文件上传多部分请求.更准确地说-这些分段请求,在要上传的文件中包含来自UTF-8的非拉丁符号(例如±čęėįš).当请求到达必须处理@RequestPart MultipartFile file的服务时,file.getOriginalFilename()会在上述符号的位置返回问号.现在,我尝试将此类文件直接上传到此类控制器,并且文件名没有问号,即没有损坏,这表明当代理中继传入的请求时,Zuul过滤器中某处发生了对多部分请求的某些错误解释/解析.

All this configuration is working with no problem. The problem now that i am facing is with file upload multipart requests. To be more precise - those multipart requests, when file to be uploaded has non latin symbols (e.g. ąčęėįš) from UTF-8. When request reaches service which has to deal with @RequestPart MultipartFile file, then file.getOriginalFilename() returns questionmarks in the places of aforementioned symbols. Now, i have tried to directly upload such file to such controller, and filename comes without questionmarks, that is, not broken, which suggests, that some bad interpretation/parsing of multipart request occurs somewhere in Zuul filters, when proxy relays incomming request.

也许有人在Zuul上也有类似的经历,可以指导我解决该问题的方法吗?

Maybe someone had similar experience with Zuul and can direct me some way to resolve this problem?

推荐答案

我自己也遇到了同一问题,并创建了以下问题:

I just ran into the same issue myself, and created the following issue:

https://jira.spring.io/browse/SPR-15396

希望这可以在Spring 4.3.8中进行配置.

Hopefully this is getting configurable in Spring 4.3.8.

同时,您必须创建一个FormBodyWrapperFilter类型的Bean(它覆盖ZuulConfiguration中的一个).在构造函数中,传递从FormHttpMessageConverter扩展的FormHttpMessageConverter副本,并将FormHttpMessageConverter.MultipartHttpOutputMessage#getAsciiBytes(String)中使用的编码更改为UTF-8(您可能还希望删除对javax-mail的任何引用,除非您将其放在类路径上).您需要使用Spring Cloud Netflix的最新版本来执行此操作.

In the meantime, you have to create a bean of type FormBodyWrapperFilter (that overrides the one in ZuulConfiguration). In the constructor, you pass a copy of FormHttpMessageConverter, which extends from FormHttpMessageConverter, and you change the encoding used in FormHttpMessageConverter.MultipartHttpOutputMessage#getAsciiBytes(String) to UTF-8 (you might also want to delete any references to javax-mail, unless you have that on classpath). You need a pretty recent version of Spring Cloud Netflix to do this.

示例:

@Bean
FormBodyWrapperFilter formBodyWrapperFilter() {
    return new FormBodyWrapperFilter(new MyFormHttpMessageConverter());
}

然后创建FormHttpMessageConverter的副本,并更改​​以下方法:

Then you create a copy of FormHttpMessageConverter, and change the following method:

    private byte[] getAsciiBytes(String name) {
        try {
            // THIS IS THE ONLY MODIFICATION:
            return name.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            // Should not happen - US-ASCII is always supported.
            throw new IllegalStateException(ex);
        }
    }

这篇关于Spring-Cloud Zuul在转发的多部分请求文件名中破坏了UTF-8符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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