question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

By default ServletOAuth2AuthorizedClientExchangeFilterFunction for ClientCredentials uses RestTemplate to request OAuth token in a blocking way

See original GitHub issue

Summary

When using ServletOAuth2AuthorizedClientExchangeFilterFunction to perform oauth requests using the Spring Webflux WebClient the filter function utilizes a blocking call using RestTemplate which blocks the calling thread.

Actual Behavior

getTokenResponse at https://github.com/spring-projects/spring-security/blob/master/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServletOAuth2AuthorizedClientExchangeFilterFunction.java#L349 using the default DefaultClientCredentialsTokenResponseClient (https://github.com/spring-projects/spring-security/blob/master/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServletOAuth2AuthorizedClientExchangeFilterFunction.java#L119) blocks.

Expected Behavior

The setup of the request should never block.

Configuration

ClientRegistration is created like this:

ClientRegistration.withRegistrationId("registration") //
                    .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) //
                    .tokenUri(uaaUrl + OAUTH_TOKEN_ENDPOINT) //
                    .clientAuthenticationMethod(ClientAuthenticationMethod.POST) //
                    .userInfoAuthenticationMethod(AuthenticationMethod.HEADER) //
                    .clientId(getClientId()) //
                    .clientSecret(getClientSecret()) //
                    .build()

actual usage of webclient then is like this

        Mono<ClientResponse> clientMono = webclient.get()
                .uri("https://somewhere")
                .attributes(clientRegistrationId("registration")).accept(MediaType.APPLICATION_JSON_UTF8)
                .exchange();
        clientMono.subscribe(); // this call blocks while initially retrieving oauth token

Version

spring-security-oauth2-client:5.1.1.RELEASE

Sample

I can provide a sample if nescessary but don’t have one right now.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
frzmecommented, Dec 7, 2018

My issue probably comes from the fact that my WebClient usage actually has little to do with the incoming requests. I’m just using it to resolve client credentials OAuth tokens, similar on how it was possible with the OAuth2RestTemplate.

I have no need to store any information in the request security context, rather I’d like to store them globally for my application (since the oauth details are not user specific), what would be my best option to enable this?

1reaction
jgrandjacommented, Dec 7, 2018

@FrzMe If your app is running in a Servlet environment than you have blocking behaviour regardless if you’re using WebClient in a reactive way. As an FYI, WebClient does support blocking behaviour via webclient.get().uri("somewhere") ....block().

If you’re using WebClient in a servlet environment than you need to use it with block(). See this example.

If you’re using WebClient in a fully reactive environment (eg. Netty) than you would not use block() and simply return a Mono or Flux. See this example.

Makes sense?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Security 5 Replacement for OAuth2RestTemplate
In my use case all I want to do is make a request with a RestTemplate to an external service that is protected...
Read more >
How to Automatically Request and Refresh OAuth2 Client ...
This post is based on this question: Does anyone have an example of using @springframework WebClient (or RestTemplate) to get an OAuth2 token...
Read more >
OAuth 2.0 Client :: Spring Security
The default implementation of OAuth2AccessTokenResponseClient for the Client Credentials grant is DefaultClientCredentialsTokenResponseClient , which uses a ...
Read more >
How to Use Client Credentials Flow with Spring Security
Learn how to use OAuth 2.0's client credentials grant to ... client that uses Spring's RestTemplate to make authenticated requests to the ...
Read more >
How to Use Spring Cloud Gateway With OAuth 2.0 Patterns
You will also learn the necessary configurations for OpenID Configuration Authentication, Token Relay, and Client Credentials Grant, all of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found