[英]Spring Resource Server with OAuth2 to relay access token
我有一個受 OAuth2 (KeyCloak) 保護的 Spring Boot 資源服務器。 我可以使用 Bearer Token 訪問端點。 現在,我想調用另一個受身份驗證服務器保護的服務。 我想中繼令牌。 我找不到關於如何做到這一點的明確指南。
我的依賴是:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
我的 application.yml 是這樣的:
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: <info>
我正在嘗試創建 OAuth2RestTemplate,例如:
@Bean
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext, OAuth2ProtectedResourceDetails details) {
return new OAuth2RestTemplate(details, oauth2ClientContext);
}
但我收到錯誤:
required a bean of type 'org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails' that could not be found.
我怎樣才能解決這個問題?
經過大量研究和反復試驗,我提出的解決方案是:
添加依賴
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
和
@EnableOAuth2Client
在 application.yml 中,我添加了
security:
oauth2:
keycloak:
clientId: <CLIENT_ID>
clientSecret: <CLIENT_SECRET>
grantType: client_credentials
accessTokenUri: <URI>
userAuthorizationUri: <URI>
scope: openid profile email
配置
@Bean
@ConfigurationProperties("security.oauth2.keycloak")
protected OAuth2ProtectedResourceDetails keycloakOAuth2Details() {
return new ClientCredentialsResourceDetails();
}
@LoadBalanced
@Bean
public OAuth2RestTemplate restTemplate(RestTemplateCustomizer customizer) {
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(keycloakOAuth2Details);
customizer.customize(restTemplate);
return restTemplate;
}
我不確定是否有必要依賴它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.