-
Notifications
You must be signed in to change notification settings - Fork 6k
Add OAuth2AuthorizedClientManager
autoconfiguration without spring-boot-starter-web
dependency
#15877
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
Comments
@yvasyliev thanks for reaching out! I think there might be some overlapping concepts regarding Spring Boot outlined in this issue, that should be clarified before we can discuss your use case.
Because of the above, some of what you ask in the issue isn't quite accurate in context. Also, I don't think this request could simply be moved to Spring Boot because much of what you're asking for here is still specific to Spring Security, but would not be implemented the way you mention above (using Spring Boot features). Regarding your use case:
When Spring Security automatically configures an
The presence of Spring Security itself is only automatically set up in Spring Boot web applications (e.g. when
I'm sorry you feel that it is too much boilerplate code. However, I think the bean configuration in your example is fairly reasonable and minimal given that what you're requesting isn't supported out of the box. Considering that the above is context for how things work now, what I think this request ends up asking is whether Spring Security can provide some kind of feature for initializing a non-web application with OAuth2 Client features, specifically using This is an interesting request and could be a compelling use case. For requests like this, we typically want to see how many users in the community are asking for this before deciding to tackle it. We do that by tracking upvotes on open issues over time and if quite a lot of community interest is demonstrated, we would decide to prioritize it at that point. Make sense? |
@sjohnr thanks for such a detailed explanation! I 100% agree. Let's see if anyone else needs this feature. 😊 |
Just in case if anyone is looking into this topic, I found a more concise configuration approach: @Configuration
@EnableConfigurationProperties(OAuth2ClientProperties.class)
public class MyServiceConfig {
@Bean
public MyService myService(OAuth2ClientProperties oAuth2ClientProperties) {
var clientRegistrations = List.copyOf(new OAuth2ClientPropertiesMapper(oAuth2ClientProperties)
.asClientRegistrations()
.values()
);
var clientRegistrationRepository = new InMemoryClientRegistrationRepository(clientRegistrations);
var authorizedClientService = new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
var authorizedClientManager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(
clientRegistrationRepository,
authorizedClientService
);
var oAuth2ClientHttpRequestInterceptor = new OAuth2ClientHttpRequestInterceptor(
authorizedClientManager,
request -> "auth-1"
);
var restClient = RestClient.builder()
.baseUrl("https://api.service-1.com")
.requestInterceptor(oAuth2ClientHttpRequestInterceptor)
.build();
var restClientAdapter = RestClientAdapter.create(restClient);
var httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
return httpServiceProxyFactory.createClient(MyService.class);
}
} |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I’m experiencing a similar issue with a Feign client. As mentioned in the Spring Cloud OpenFeign documentation, the solution is to enable the flag spring.cloud.openfeign.oauth2.enabled=true in the application.yml or application.properties. However, when running the application, I see the following log message in the console (debug mode enabled):
It seems no interceptor is being invoked during Feign client requests. Additionally, my application does not and should not have any dependencies related to spring-boot-starter-web. Any tips? Thanks. |
@AndreaLombardo I think you will need to reach out on the spring cloud project’s issue tracker if you believe it’s a bug. Spring Security does not contribute to auto-configuration since it is not built on Spring Boot. |
I'm having the exact same usecase as @yvasyliev and was scratching my head a while before finding this issue which finally made things clear thanks to @sjohnr's good and detailed explanation, thanks a lot to both! I would be very interested in the new feature as @sjohnr suggested. Thinking about it maybe it could be done a little bit broader because I also copied the following code to some of my web applications:
The use case would be / is "I want to make requests to an oauth2-protected external resource using my applications credentials (i.e. using the For people also stumbling over this here some details to the given explanation above (why it isnt working when not an web-app, it indeed seems quite magical to anyone who never looked into this):
|
For reference (similar to above comments), here is the most minimal configuration that I am aware of for configuring OAuth2 Client to obtain a Besides It seems plausible for something like this to be provided, possibly as a separate Spring Boot starter, with some auto-configuration for this specific scenario. However, I'm not sure what @philwebb Does Spring Boot have any scenarios for auto-configuration that's activated when an application is "not a web app"? |
@sjohnr We have |
See also spring-projects/spring-boot#43978 related to the same issue for resource servers |
Interesting, thanks! (Thinking out loud) So I suppose a |
Perhaps we can add a dependency in |
Actually, that might make sense. Typically, the json support that comes with web or webflux is enough, but since OAuth2 Client is built around making OAuth 2.0 Access Token Requests which return JSON, we do need json support pretty much all the time. |
Uh oh!
There was an error while loading. Please reload this page.
Expected Behavior
I would like
org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager
to be an autoconfigured bean based onapplication.yml
properties, and without havingspring-boot-starter-web
dependency.My desirable state would be the following:
pom.xml
application.yml
MyServiceConfig.java
I would expect
oAuth2AuthorizedClientManager
to be an instance oforg.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager
, because it exists outside the servlet context.Current Behavior
The application above fails to start:
Console output
If I add
spring-boot-starter-web
dependency to the project, theoAuth2AuthorizedClientManager
bean will be automatically created:pom.xml
But at the same time I'm having:
oAuth2AuthorizedClientManager
is instance oforg.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager
.spring.main.web-application=none
property will disable server startup andOAuth2AuthorizedClientManager
autoconfiguration.Context
I'm building a Spring Boot (not web!) application that communicates with external REST services. I want to utilize HTTP Interface based on
RestClient
with OAuth interceptor. And I don't really want to addspring-boot-starter-web
to my project, because it includes HTTP server that I won't use.It would be awesome if
OAuth2AuthorizedClientManager
bean was automatically created in case ofspring.security.oauth2.client.*
properties existence inapplication.yml
just likespring-boot-starter-web
does.I can achieve the desired outcome by manual
OAuth2AuthorizedClientManager
configuration:MyServiceConfig.java
But there's too much boilerplate code.
The text was updated successfully, but these errors were encountered: