我正在尝试将 spring-boot-starter-oauth2-client 与新的 Spring Framework 版本 6 HttpInterface 一起使用
@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
以上有效。 我们从令牌提供者那里得到了确认,我们得到了令牌,并且从资源服务器那里得到了资源,并传递了令牌。 两个步骤都运行良好,快乐。
https://www.youtube.com/watch?v=aR580OCEp7w 我们现在希望使用新的 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<String, String> key value);
}
@GetMapping("/lessons")
public User fetchLessons() {
return userClient.getUserById(Map.of("foo", "bar"));
}
当使用HttpInterface时,这不起作用。 首先并没有获取令牌。 可能是因为 @HttpExchange @GetExchange 缺少 .attributes(clientRegistrationId("id")) ,但不确定。
问题:如何将 Http Interface 与 spring-boot-starter-oauth2-client token 结合起来?
您应该在您的
setClientRegistrationIdResolver
实例上 OAuth2ClientHttpRequestInterceptor
:
final var interceptor = new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
interceptor.setClientRegistrationIdResolver((HttpRequest request) -> "my-client");
这就是我在 我的启动器中所做的,它有助于自动配置
RestClient
和 WebClient
beans 以及各种请求授权机制(Basic
、Bearer
和 API 密钥)和 HTTP 代理,仅使用应用程序属性。在你的情况下,这会给出:
spring:
application:
name: client-application
security:
oauth2:
client:
registration:
my-registration:
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
com:
c4-soft:
springaddons:
rest:
client:
user:
base-url: https://host.com
authorization:
oauth2:
oauth2-registration-id: my-registration
@Configuration
public class RestConfiguration {
@Bean
// userClient is auto-configured by spring-addons-starter-rest
// using com.c4-soft.springaddons.rest.client.user properties
UserApi userApi(RestClient userClient) throws Exception {
return new RestClientHttpExchangeProxyFactoryBean<>(UserApi .class, userClient).getObject();
}
}
@RestController
@RequiredArgsConstructor
public class LessonsController {
private final UserApi userApi;
}
是不是很牛逼?
请注意,为了清楚起见,我将其重命名为:
my-client
到 my-registration
@HttpExchange
到 UserClient
的 UserApi
(userClient
用作自动配置的 RestClient
@Bean
代理内部使用的名称)