Code: Select all
private final RestClient restClient;
public Controller(RestClient.Builder restClientBuilder) {
this.restClient = restClientBuilder.build();
}
@GetMapping("/lessons")
public String fetchLessons() {
Map ssa = restClient.post()
.uri("https://service.com/token?scope=resolve+download&grant_type=client_credentials")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.headers(httpHeaders -> httpHeaders.setBasicAuth("aaa", "bbb"))
.retrieve()
.body(Map.class);
final MultiValueMap params2 = new LinkedMultiValueMap();
params2.add("Authorization", "Bearer " + ssa.get("access_token"));
Consumer consumer2 = it -> it.addAll(params2);
String result = restClient.get()
.uri("https://protectedresource...")
.headers(consumer2)
.retrieve()
.body(String.class);
return result;
}

Alles funktioniert, ich bin zufrieden. In der Hoffnung, Spring Boot OAuth2 zu nutzen, verfügen wir jetzt über diesen Code
Code: Select all
@Configuration
public class RestClientConfig {
@Bean
public RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager, RestClient.Builder restClientBuilder) {
OAuth2ClientHttpRequestInterceptor interceptor = new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
return restClientBuilder
.requestInterceptor(interceptor)
.build();
}
Code: Select all
@RestController
public class LessonsController {
private final RestClient restClient;
public LessonsController(RestClient restClient) {
this.restClient = restClient;
}
@GetMapping("/lessons")
public String fetchLessons() {
return restClient.get()
.uri("https://someprotectedresourceneedingtoken")
.attributes(clientRegistrationId("my-provider"))
.retrieve()
.body(String.class);
}
}
Code: Select all
spring:
application:
name: client-application
security:
oauth2:
client:
registration:
my-provider:
provider: the-provider
client-id: someusername
client-secret: somepassword
authorization-grant-type: client_credentials
scope:
- resolve
- download
provider:
the-provider:
token-uri: https://somethirdparty.com/token

Code: Select all
2025-01-09T23:59:01.308+08:00 DEBUG 4250 --- [client-application] [nio-8080-exec-2] [29e0fac8ee9435452ddc0e91ac67b652-9c95bddc85f879bb] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@10ce6bfd 8 pairs: {POST /token HTTP/1.1: null}{Accept: application/json;charset=UTF-8}{Content-Type: application/x-www-form-urlencoded;charset=UTF-8}{...5}{User-Agent: Java/23}{Host: ....com}{Connection: keep-alive}{Content-Length: 76}
2025-01-09T23:59:01.420+08:00 DEBUG 4250 --- [client-application] [nio-8080-exec-2] [29e0fac8ee9435452ddc0e91ac67b652-9c95bddc85f879bb] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@67f01113 13 pairs: {null: HTTP/1.1 200}{Date: Thu, 09 Jan 2025 15:59:01 GMT}{Content-Type: application/json;charset=UTF-8}{Transfer-Encoding: chunked}{Connection: keep-alive}{Vary: origin,access-control-request-method,access-control-request-headers,accept-encoding}{X-Content-Type-Options: nosniff}{X-XSS-Protection: 0}{Cache-Control: no-cache, no-store, max-age=0, must-revalidate}{Pragma: no-cache}{Expires: 0}{Strict-Transport-Security: max-age=31536000 ; includeSubDomains}{X-Frame-Options: DENY}
2025-01-09T23:59:01.420+08:00 DEBUG 4250 --- [client-application] [nio-8080-exec-2] [29e0fac8ee9435452ddc0e91ac67b652-9c95bddc85f879bb] o.s.web.client.RestTemplate : Response 200 OK
2025-01-09T23:59:01.421+08:00 DEBUG 4250 --- [client-application] [nio-8080-exec-2] [29e0fac8ee9435452ddc0e91ac67b652-9c95bddc85f879bb] o.s.web.client.RestTemplate : Reading to [org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse] as "application/json;charset=UTF-8"
Fragen:
- Wie erhält man mit Lösung 2 Beobachtbarkeit (Spuren, aber auch Metriken)?
- Das meiste davon Alles, wie ich meinen eigenen RestClient übergebe (nicht RestTemplate, nicht WebClient) für den Token-Aufruf?