Spring-Cloud Zuul 在转发的多部分请求文件名中破坏 UTF-8 符号

Pau*_*nas 5 utf-8 spring-boot microservices netflix-eureka netflix-zuul

这对我来说是第一次,所以请耐心回答我的第一个问题。

我想我有某种配置问题,但经过一天的实验后,我被卡住了。我们的应用程序基于 Spring-Cloud [Brixton 版本]。我们有这样的配置:Portal(提供基于 angular 的 web-ui 的 web 应用程序),它有 zuul 代理,单路由配置到我们的网关服务,如下所示:

zuul:
   ignoredServices: '*'
   prefix: /api
   routes:
       api-proxy:
          path: /**
          serviceId: api-gateway
Run Code Online (Sandbox Code Playgroud)

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

zuul:
  ignoredServices: '*'
  routes:
     service1:
       path: /service1/**
       serviceId: service1
     service2:
       path: /service2/**
       serviceId: service2
Run Code Online (Sandbox Code Playgroud)

所有这些配置都没有问题。我现在面临的问题是文件上传分段请求。更准确地说 - 那些多部分请求,当要上传的文件具有来自UTF-8. 当请求到达必须处理的服务时@RequestPart MultipartFile file,则file.getOriginalFilename()在上述符号的位置返回问号。现在,我尝试将此类文件直接上传到此类控制器,并且文件名没有问号,即未损坏,这表明当代理中继传入请求时,Zuul 过滤器中某处发生了对多部分请求的某些错误解释/解析。

也许有人对 Zuul 有类似的经验,可以指导我解决这个问题吗?

And*_*and 3

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

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

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

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

例子:

@Bean
FormBodyWrapperFilter formBodyWrapperFilter() {
    return new FormBodyWrapperFilter(new MyFormHttpMessageConverter());
}
Run Code Online (Sandbox Code Playgroud)

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

    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);
        }
    }
Run Code Online (Sandbox Code Playgroud)