Webflux Spring 2.1.2 настраивает Content-Type - PullRequest
1 голос
/ 14 марта 2019

Я пытаюсь отправить сообщение через WebClient для получения токена Microsoft:

public WebClient getWebclient() {
    TcpClient client = TcpClient.create()
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
            .doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(15)).addHandlerLast(new WriteTimeoutHandler(15)));

    ExchangeStrategies strategies = ExchangeStrategies.builder()
            .codecs(configurer -> {
                configurer.registerDefaults(true);
                FormHttpMessageReader formHttpMessageReader = new FormHttpMessageReader();
                formHttpMessageReader.setEnableLoggingRequestDetails(true);
                configurer.customCodecs().reader(formHttpMessageReader);
            })
            .build();

    return WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(HttpClient.from(client).followRedirect(true)))
            .exchangeStrategies(strategies)
            .filter(logRequest())
            .filter(logResponse())
            .build();
}

MultiValueMap<String, String> credentials = new LinkedMultiValueMap<>();
        credentials.add("grant_type", "password");
        credentials.add("client_id", oauthClientId);
        credentials.add("resource", oauthResource);
        credentials.add("scope", oauthScope);
        credentials.add("username", oauthUsername);
        credentials.add("password", oauthPassword);

        Mono<MicrosoftToken> response = webClientService.getWebclient().post()
                .uri(oauthUrl)
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .body(BodyInserters.fromFormData(credentials))
                .retrieve()
                .onStatus(HttpStatus::is4xxClientError, clientResponse ->
                        Mono.error(new WebClientException(clientResponse.bodyToMono(String.class), clientResponse.statusCode())))
                .bodyToMono(MicrosoftToken.class);

        this.cachedToken = response.block();

Проблема заключается в том, что Microsoft не может обработать Тип содержимого: application / x-www-form-urlencoded;charset = UTF-8 .

Spring автоматически добавляет charset = UTF-8 в запрос.Мне нужно избавиться от этой дополнительной кодировки.Мне нужен Content-Type: application / x-www-form-urlencoded .Это возможно?В противном случае мне нужно понизить мою весеннюю версию до 2.0.0, где кодировка не добавляется автоматически.

Мои журналы отладки распечатать:

2019-03-14 10:08:42 DEBUG [reactor.netty.channel.ChannelOperationsHandler]: 
[id: 0x5d6effce, L:/192.168.148.14:52285 - 
R:login.microsoftonline.de/51.4.136.42:443] Writing object 
DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
POST /common/oauth2/token HTTP/1.1
user-agent: ReactorNetty/0.8.4.RELEASE
host: login.microsoftonline.de
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 205
2019-03-14 10:08:42 DEBUG [reactor.netty.channel.ChannelOperationsHandler]: 
[id: 0x5d6effce, L:/192.168.148.14:52285 - 
R:login.microsoftonline.de/51.4.136.42:443] Writing object 

Я проверял это с весенней версией 2.0.0и там кодировка не добавляется как в новой версии:

POST /common/oauth2/token HTTP/1.1
user-agent: ReactorNetty/0.7.5.RELEASE
host: login.microsoftonline.de
accept-encoding: gzip
Content-Type: application/x-www-form-urlencoded
Content-Length: 205
...