-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RestClient
interceptor
#15437
Add RestClient
interceptor
#15437
Conversation
Hey. If using an HTTP Interface, how would it be possible to dynamically change In webclient I could use ServletOAuth2AuthorizedClientExchangeFilterFunction: @Bean
public WebClient webClient(
WebClient.Builder builder,
HttpClient httpClient,
OAuth2AuthorizedClientManager authorizedClientManager
) {
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2 =
new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
oauth2.setDefaultOAuth2AuthorizedClient(true);
return builder
.apply(oauth2.oauth2Configuration())
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
} |
Thanks for asking, @azdanov. See the per-request arrangement in this comment. This aspect of the interceptor is still in need of feedback so feel free to weigh in on it. The other option would be to use However, I'm not clear how you would dynamically change the |
...framework/security/oauth2/client/web/function/client/OAuth2ClientHttpRequestInterceptor.java
Outdated
Show resolved
Hide resolved
This is what I meant, thank you for clarifying things 🙂. That's what I considered dynamic, that the interceptor will do it, and not me. It would be cool to see a |
Thanks @azdanov, I suspected so but didn't want to assume. I have not yet determined whether it is truly necessary to introduce this functionality into the interceptor. I consider it a convenience feature, and it creates more ways to use the interceptor which can confuse users as much as help them. That's something I'd like to avoid. However, this is good feedback so I will think about it some more. |
@sjohnr I've tried to debug it, and I guess the root cause of the problem is that ur code tried to get the RequestAttributes from the RequestContextHolder and then get the request from the RequestAttributes. Since @async is using, there is nothing in the RequestContextHolder... => failed ~ Could you do me a favour and suggest me a way to fix it? |
To use OAuth2 Client features with @Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientService authorizedClientService) {
return new AuthorizedClientServiceOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientService);
} I believe that will solve the issue, but note that this wouldn't be an issue with the interceptor but only with your configuration. If this doesn't solve the issue, please provide a minimal sample as I am not able to reproduce your issue without this. |
c875f2c
to
5caf889
Compare
Just a note for those following along: Based on feedback, I've pushed changes to the PR (gh-15437) with an updated API for the interceptor that takes advantage of import static org.springframework.security.oauth2.client.web.function.client.OAuth2ClientHttpRequestInterceptor.clientRegistrationId;
@GetMapping("/cashcards")
public Cashcard[] cashcards() {
OAuth2ClientHttpRequestInterceptor requestInterceptor =
new OAuth2ClientHttpRequestInterceptor(this.authorizedClientManager);
// Configure the interceptor to remove invalid authorized clients
requestInterceptor.setAuthorizedClientRepository(this.authorizedClientRepository);
return this.restClient.get()
.uri("http://localhost:8090/cashcards")
.attributes(clientRegistrationId(CLIENT_REGISTRATION_ID))
.retrieve()
.body(Cashcard[].class);
} Note that you can also call cc: @azdanov @barbetb @mcordeiro73 @ThomasVitale @mjeffrey @ThomasKasene @rwinch @jgrandja |
5caf889
to
a3d61bd
Compare
@sjohnr
Steps To Reproduce:
Expected Behavior: The client app should discard the invalid token and fetch a new one. |
@Lonelysoul-HayashiNoMirai thank you again for trying the interceptor, and providing samples for the issue you encountered. There are a couple of things going on.
Again, thank you for trying things out and looking for issues. It is very helpful to know that this use case requires careful setup in order to avoid issues, and I think clear documentation will help immensely. |
@sjohnr Point 1: "You have modified..." It works flawlessly now. |
It is expected behavior, yes. Adding retry would be pretty cool, let me know if that works for you! |
2dcb92d
to
cb7c727
Compare
@sjohnr |
To Do:
Closes gh-13588