我不知道如何一起使用。用户通过身份验证后,我在上下文中具有OAuth2AuthorizedClient,然后可以使用它:
String clientRegistrationId = auth.getauthorizedClientRegistrationId();
OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(clientRegistrationId,auth.getName());
accessToken = client.getaccessToken().getTokenValue();
因此,我可以在应用程序中使用accessToken。但这是我注入RestTemplate的手动拦截器。同样,如果我这样做,则必须使用另一个拦截器处理所有的refreshToken逻辑。然后是OAuth2RestTemplate,它处理所有我不需要拦截器的accessToken和refresToken东西。但是要使用它,我需要OAuth2ProtectedResourceDetails:
public OAuth2RestTemplate(OAuth2ProtectedResourceDetails resource) {
this(resource,new DefaultOAuth2ClientContext());
}
,如果我使用spring.security.oauth2.client.registration样式,则不会。因为仅在使用security.oauth2.client样式时才创建AuthorizationCodeResourceDetails。:
spring:
thymeleaf:
cache: false
security:
oauth2:
client:
registration:
example-authorization-code:
id: demo-webapp-oidc-code
client-id: demo-webapp-oidc-code
client-secret: secret
client-name: demo-webapp-oidc-code
provider: authServer
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
authorization-grant-type: authorization_code
client-authentication-method: post
scope: openid,address
provider:
authServer:
token-uri: http://localhost:8095/authServer/oidc/token
authorization-uri: http://localhost:8095/authServer/oidc/authorize
user-info-uri: http://localhost:8095/authServer/oidc/profile
jwk-set-uri: http://localhost:8095/authServer/oidc/jwks
user-name-attribute: id
这是它接受的方式:
@Bean
@ConfigurationProperties(prefix = "security.oauth2.client")
@Primary
public AuthorizationCodeResourceDetails oauth2RemoteResource() {
return new AuthorizationCodeResourceDetails();
}
我可以手动创建它,但是事情变得复杂了。让我们想象一下,我创建了resourceDetails,而不是创建时花了OAuth2RestTemplate,
new OAuth2RestTemplate( resourceDetails,defaultOAuth2ClientContext);
这意味着我需要设置新的DefaultOAuth2ClientContext:
public DefaultOAuth2ClientContext(OAuth2accessToken accessToken) {
this.accessToken = accessToken;
this.accessTokenRequest = new DefaultaccessTokenRequest();
}
,需要Oauth2accessToken。但是我在上下文中具有 core.OAuth2accessToken (OAuth2AuthorizedClientauthorizedClient.getaccessToken),而它需要 common.OAuth2accessToken。