Код: Выделить всё
@Configuration
public class RestClientConfig {
@Bean
public RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager, RestClient.Builder restClientBuilder) {
OAuth2ClientHttpRequestInterceptor interceptor = new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
return restClientBuilder
.requestInterceptor(interceptor)
.build();
}
}
Код: Выделить всё
@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://someserver.om/someprotectedresource")
.attributes(clientRegistrationId("my-client"))
.retrieve()
.body(String.class);
}
}
Код: Выделить всё
spring:
application:
name: client-application
security:
oauth2:
client:
registration:
my-client:
provider: my-provider
client-id: ididid
client-secret: secretsecret
authorization-grant-type: client_credentials
scope: download
provider:
my-provider:
token-uri: https://provider.com/token
У нас есть подтверждение от поставщика токенов, что мы получили токен, а также от сервера ресурсов, который мы получили, передав токен.
/>Оба двух шага работают нормально, доволен.
Теперь мы хотели бы сделать то же самое, с новым выпуском Spring Framework 6 HttpInterface
При этом:
Код: Выделить всё
@Configuration
public class UserClientConfig {
private final RestClient restClient;
public UserClientConfig(OAuth2AuthorizedClientManager authorizedClientManager, RestClient.Builder restClientBuilder) {
OAuth2ClientHttpRequestInterceptor interceptor = new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
this.restClient = restClientBuilder
.requestInterceptor(interceptor)
.baseUrl("https://host.com")
.build();
}
@Bean
public UserClient userClient() {
RestClientAdapter adapter = RestClientAdapter.create(restClient);
return HttpServiceProxyFactory.builderFor(adapter)
.build()
.createClient(UserClient.class);
}
}
Код: Выделить всё
@HttpExchange(
url = "/v1",
accept = MediaType.APPLICATION_JSON_VALUE)
public interface UserClient {
@GetExchange("/protectedresource/full")
public User getUserById(@RequestParam Map key value);
}
Код: Выделить всё
@GetMapping("/lessons")
public User fetchLessons() {
return userClient.getUserById(Map.of("foo", "bar"));
}
Токен вообще не извлекается.
Возможно, из-за отсутствия .attributes(clientRegistrationId(" id")) для @HttpExchange @GetExchange, но не уверен.
Вопрос: как совместить Http Interface с токен Spring-boot-starter-oauth2-client?
Подробнее здесь: https://stackoverflow.com/questions/793 ... ework-rele