Skip to content

Instantly share code, notes, and snippets.

@krnbr
Last active July 19, 2020 03:17
Show Gist options
  • Select an option

  • Save krnbr/e5e7bd4cf43ac91f0cfe076b7a73eaac to your computer and use it in GitHub Desktop.

Select an option

Save krnbr/e5e7bd4cf43ac91f0cfe076b7a73eaac to your computer and use it in GitHub Desktop.
Oauth2 Client Configuration
@Configuration
public class TestClientConfig {
@Value("${test.client.base.url}")
private String testClientBaseUrl;
private Logger testWebClientLogger = LoggerFactory.getLogger("TEST_WEB_CLIENT");
/**
* The authorizedClientManager for required by the webClient
*/
@Bean
public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(final ReactiveClientRegistrationRepository clientRegistrationRepository,
final ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
.clientCredentials()
.build();
DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager = new DefaultReactiveOAuth2AuthorizedClientManager(clientRegistrationRepository, authorizedClientRepository);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
return authorizedClientManager;
}
/**
* The Oauth2 based WebClient bean for the web service
*/
@Bean("testWebClient")
public WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
String registrationId = "local";
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
// for telling which registration to use for the webclient
oauth.setDefaultClientRegistrationId(registrationId);
return WebClient.builder()
// base path of the client, this way we need to set the complete url again
.baseUrl(testClientBaseUrl)
.filter(oauth)
.filter(logRequest())
.filter(logResponse())
.build();
}
/*
* Log request details for the downstream web service calls
*/
private ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(c -> {
testWebClientLogger.info("Request: {} {}", c.method(), c.url());
c.headers().forEach((n, v) -> {
if (!n.equalsIgnoreCase(AUTHORIZATION)) {
testWebClientLogger.info("request header {}={}", n, v);
} else {
// as the AUTHORIZATION header is something security bounded
// will show up when the debug level logging is enabled
// for example using property - logging.level.root=DEBUG
testWebClientLogger.debug("request header {}={}", n, v);
}
});
return Mono.just(c);
});
}
/*
* Log response details for the downstream web service calls
*/
private ExchangeFilterFunction logResponse() {
return ExchangeFilterFunction.ofResponseProcessor(c -> {
testWebClientLogger.info("Response: {} {}", c.statusCode());
// if want to show the response headers in the log by any chance?
/*c.headers().asHttpHeaders().forEach((n, v) -> {
testWebClientLogger.info("response header {}={}", n, v);
});*/
return Mono.just(c);
});
}
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
return http.oauth2Client().and().build();
}
}
@krnbr

krnbr commented Jul 19, 2020

Copy link
Copy Markdown
Author

comments added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment