All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.c4_soft.springaddons.rest.ReactiveSpringAddonsWebClientSupport Maven / Gradle / Ivy

There is a newer version: 7.9.0-M4
Show newest version
package com.c4_soft.springaddons.rest;

import java.util.Optional;

import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientManager;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.service.annotation.HttpExchange;

import reactor.core.publisher.Mono;

/**
 * 

* Provides with {@link WebClient} builder instances pre-configured with: *

*
    *
  • HTTP conector if proxy properties or environment variables are set
  • *
  • Base URL
  • *
  • authorization exchange function if Basic or OAuth2 Bearer
  • *
*

*

* Also provides with helper methods to get {@link HttpExchange @@HttpExchange} proxies with {@link WebClient} *

*

* /!\ Auto-configured only in servlet (WebMVC) applications and only if some {@link SpringAddonsRestProperties} are present /!\ *

* * @author Jerome Wacongne chl4mp@c4-soft.com * @see ReactiveSpringAddonsWebClientSupport an equivalent for reactive (Webflux) applications */ public class ReactiveSpringAddonsWebClientSupport extends AbstractSpringAddonsWebClientSupport { private final Optional authorizedClientManager; public ReactiveSpringAddonsWebClientSupport( SystemProxyProperties systemProxyProperties, SpringAddonsRestProperties addonsProperties, BearerProvider forwardingBearerProvider, Optional authorizedClientManager) { super(systemProxyProperties, addonsProperties, forwardingBearerProvider); this.authorizedClientManager = authorizedClientManager; } @Override protected ExchangeFilterFunction oauth2RegistrationFilter(String registrationId) { return (ClientRequest request, ExchangeFunction next) -> { final var provider = Mono.justOrEmpty(authorizedClientManager.map(acm -> new ReactiveAuthorizedClientBearerProvider(acm, registrationId))); return provider.flatMap(ReactiveAuthorizedClientBearerProvider::getBearer).defaultIfEmpty("").flatMap(bearer -> { if (StringUtils.hasText(bearer)) { final var modified = ClientRequest.from(request).headers(headers -> headers.setBearerAuth(bearer)).build(); return next.exchange(modified); } return next.exchange(request); }); }; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy