Webflux Spring 2.1.2自定义内容类型

时间:2019-03-14 11:32:00

标签: spring spring-webflux

我正试图通过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:应用程序/ 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

2 个答案:

答案 0 :(得分:0)

有两种方法可以做到:

webClient
   .mutate()
   .defaultHeaders(headers -> {
      headers.add("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType()
   }).build()
   . uri(uri)
   ...

OR 

webClient
   .post()
   .uri(uri)
   .body(body)
   .headers(headers -> getHttpHeaders())
   ...

private HttpHeaders getHttpHeaders(){
   HttpHeaders headers = new HttpHeaders();
   headers.add("Content-Type", "application/x-www-form-urlencoded")
   return headers;
}

只有几种方法可以利用.headers或.defaultHeaders中的标头使用者。

但是我不认为字符集是老实的问题。如果您在响应中收到application / json,则可能是因为Microsoft通过您在应用注册中指定的重定向URL转发带有该标头的请求。

好消息是,这可能是可取的,因为Microsoft将令牌字段返回为json,这使您可以调用.bodyToMono(MicrosoftToken)。我记得BodyInserters.fromFormData有问题,因为它实际上并未在MultiValueMap中对值进行编码。

这是我正在使用的:

    private BodyInserter<String, ReactiveHttpOutputMessage> getBodyInserter(Map<String,String> parameters) {

        credentials.add("grant_type", encode("password"));
        credentials.add("client_id", encode(oauthClientId));
        credentials.add("resource", encode(oauthResource));
        // and so on..
        // note that parameters is a regular Map - not a MultiValueMap

        BodyInserter<String, ReactiveHttpOutputMessage> bodyInserter = BodyInserters.fromObject(
                parameters.entrySet().stream()
                        .map(entry -> entry.getKey().concat("=").concat(entry.getValue()))
                        .collect(Collectors.joining("&", "", "")));

        return bodyInserter;
    }

    private String encode(String str) {
        try {
            return URLEncoder.encode(str, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            log.error("Error encoding req body", e);
        }
    }

答案 1 :(得分:0)

这花了我一个上午的大部分时间来找出答案,但是我终于做到了。问题是Webflux BodyInserters.fromFormData 始终始终将内容类型设置为application/x-www-form-urlencoded;charset=...,无论您在标题中设置什么。

要解决此问题,请先定义此方法:

/**
 * This method is unfortunately necessary because of Spring Webflux's propensity to add {@code ";charset=..."}
 * to the {@code Content-Type} header, which the Generic Chinese Device doesn't handle properly.
 *
 * @return a {@link FormInserter} that doesn't add the character set to the content type header
 */
private FormInserter<String> formInserter() {

    return new FormInserter<String>() {

        private final MultiValueMap<String, String> data = new LinkedMultiValueMap<>();

        @Override public FormInserter<String> with(final String key, final String value) {
            data.add(key, value);
            return this;
        }

        @Override public FormInserter<String> with(final MultiValueMap<String, String> values) {
            data.addAll(values);
            return this;
        }

        @Override public Mono<Void> insert(final ClientHttpRequest outputMessage, final Context context) {
            final ResolvableType formDataType =
                    ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class);
            return new FormHttpMessageWriter() {
                @Override protected MediaType getMediaType(final MediaType mediaType) {
                    if (MediaType.APPLICATION_FORM_URLENCODED.equals(mediaType)) {
                        return mediaType;
                    } else {
                        return super.getMediaType(mediaType);
                    }
                }
            }.write(Mono.just(this.data), formDataType,
                    MediaType.APPLICATION_FORM_URLENCODED,
                    outputMessage,
                    context.hints());
        }
    };
}

然后,要调用您的Web服务,请执行以下操作:

final SomeResponseObject response = WebClient
        .builder()
        .build()
        .post()
        .uri(someOrOtherUri)
        .body(formInserter().with("param1", "value1")
                            .with("param2", "value2")
        )
        .retrieve()
        .bodyToFlux(SomeReponseObject.class)
        .blockLast();

请注意,上面的块主要用于演示目的。您可能希望也可能不想阻止并等待响应。