我有一个使用Spring Boot构建的现有REST API。在服务层中的其中一个函数上,我需要调用一个受OAuth2(client-credentials)保护的外部REST服务。
使用Spring Boot 2.3,我意识到OAuth2RestTemplate
已被弃用,所以我使用了WebClient
。
按照这个教程 - https://www.baeldung.com/spring-webclient-oauth2,我现在有了以下WebClientConfig
类:
@Configuration
class WebClientConfig {
@Bean
fun webClient(
clientRegistrations: ClientRegistrationRepository?,
authorizedClients: OAuth2AuthorizedClientRepository?): WebClient? {
val oauth2 = ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients)
oauth2.setDefaultOAuth2AuthorizedClient(false)
oauth2.setDefaultClientRegistrationId("test")
return WebClient.builder()
.apply(oauth2.oauth2Configuration())
.build()
}
}
在我的属性文件中,我有:
spring:
security:
oauth2:
client:
registration:
test:
client-id: <redacted>
client-secret: <redacted>
authorization-grant-type: client_credentials
provider:
test:
token-uri: <redacted>
我甚至无法确定这是否有效,因为当我访问与此OAuth2身份验证无关的API上的不同端点时,会不断收到以下错误提示:
java.lang.IllegalArgumentException: Invalid Authorization Grant Type (client_credentials) for Client Registration with Id: test
我快要崩溃了,因为我无法解决这个问题...任何帮助将不胜感激!谢谢!
sniac
重命名为test
。 - bilak