Используйте Spring-boot-starter-oauth2-client для передачи токена в Spring Framework версии 6 HttpInterface @HttpExchangJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Используйте Spring-boot-starter-oauth2-client для передачи токена в Spring Framework версии 6 HttpInterface @HttpExchang

Сообщение Anonymous »

Я пытаюсь использовать Spring-boot-starter-oauth2-client с новым HttpInterface Spring Framework версии 6

Код: Выделить всё

@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"));
}
При использовании HttpInterface это не сработает.
Токен вообще не извлекается.
Возможно, из-за отсутствия .attributes(clientRegistrationId(" id")) для @HttpExchange @GetExchange, но не уверен.
Вопрос: как совместить Http Interface с токен Spring-boot-starter-oauth2-client?

Подробнее здесь: https://stackoverflow.com/questions/793 ... ework-rele
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Настраиваемое пользовательское кэширование TTL для токена Spring-boot-starter-oauth2-client
    Anonymous » » в форуме JAVA
    0 Ответы
    16 Просмотры
    Последнее сообщение Anonymous
  • Spring-Boot-Starter-AMQP с использованием Spring-Boot-Starter-AMQP
    Anonymous » » в форуме JAVA
    0 Ответы
    38 Просмотры
    Последнее сообщение Anonymous
  • Spring-Boot-Starter-AMQP с использованием Spring-Boot-Starter-AMQP
    Anonymous » » в форуме JAVA
    0 Ответы
    32 Просмотры
    Последнее сообщение Anonymous
  • Spring-Boot-Starter-AMQP с использованием Spring-Boot-Starter-AMQP
    Anonymous » » в форуме JAVA
    0 Ответы
    15 Просмотры
    Последнее сообщение Anonymous
  • Spring-Boot-Starter-AMQP с использованием Spring-Boot-Starter-AMQP
    Anonymous » » в форуме JAVA
    0 Ответы
    21 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»