spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.51k stars 3.31k forks source link

[Spring Cloud Gateway MVC] Multipart data missing #3527

Open lukaszpy opened 6 days ago

lukaszpy commented 6 days ago

Describe the bug After routing multipart/form-data through gateway mvc, downstream service receive request but without multipart file.

I looked into code of gateway and in RestClientProxyExchange we have

@Override
    public ServerResponse exchange(Request request) {
        return restClient.method(request.getMethod()).uri(request.getUri())
                .headers(httpHeaders -> httpHeaders.putAll(request.getHeaders()))
                .body(outputStream -> copyBody(request, outputStream))
                .exchange((clientRequest, clientResponse) -> doExchange(request, clientResponse), false);
    }

there is not copy multipart data from original request.

Example route:

@Bean
    fun dataStorageFileUpload(): RouterFunction<ServerResponse> {
        val servicesUrl = services.url!!
        Objects.requireNonNull(servicesUrl["dataStorageService"], "Data storage service url is empty")
        return GatewayRouterFunctions.route("dataStorageFileUpload")
            .PUT("/storage/file/**", http(servicesUrl["dataStorageService"]))
            .before(BeforeFilterFunctions.stripPrefix(1))
            .filter(HandlerFilterFunction.ofRequestProcessor(authenticationPopular))
            .after(AfterFilterFunctions.removeResponseHeader(GROUP_HEADER))
            .after(AfterFilterFunctions.removeResponseHeader(HttpHeaders.AUTHORIZATION))
            .after(AfterFilterFunctions.removeResponseHeader(TOKEN_HEADER))
            .build()
    }
dgradecak commented 20 hours ago

@lukaszpy do you have spring security configured? I had the smae behavior when using spring security, did not dig deeper to understand the cause. For now by removing spring security the body is processed correctly, specially when using spring-cloud-gateway-server-mvc

lukaszpy commented 19 hours ago

@dgradecak yes I have spring sec, as validate tokens, and checking roles for path