Description
If you want to integration test a resource server you need the OAuth2 client, but when you add that (even just in test scope) it changes the security configuration of a Spring Boot application by default - it is no longer a resource server, but instead it becomes an OAuth2 client (and installs an OAuth2 login filter instead of the resource server filter).
Also, it's kind of a pain to set up a RestClient
with a bearer token for the test. You have to create an interceptor. With client credentials it looks like this:
@Bean
@Lazy
public RestClient personService(RestClient.Builder builder, ClientRegistrationRepository repository,
OAuth2AuthorizedClientService service,
@Value("http://localhost:${local.server.port:8080}") String url) {
AuthorizedClientServiceOAuth2AuthorizedClientManager manager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(
repository, service);
OAuth2ClientHttpRequestInterceptor interceptor = new OAuth2ClientHttpRequestInterceptor(manager);
interceptor.setClientRegistrationIdResolver(request -> "spring");
builder.baseUrl(url).requestInterceptor(interceptor);
return builder.build();
}
Ideally we'd like a way to tell Spring Boot that to set that stuff up: 1) switch off the OAuth2SecurityFilterChainConfiguration
(currently not visible and not an independent autoconfig, so you can't actually exclude it); 2) make it easier to create an HTTP client.
Sort of related to #43978 but this is for a webapp that is itself a resource server.