OAuth 2.0 客户端

OAuth 2.0 客户端功能为 OAuth 2.0 授权框架中定义的客户端角色提供支持。spring-doc.cn

概括地说,可用的核心功能包括:spring-doc.cn

客户端身份验证支持
HTTP 客户端支持(用于请求受保护的资源)

DSL 提供了许多配置选项,用于自定义 OAuth 2.0 客户端使用的核心组件。 此外,还启用授权码授予的自定义。HttpSecurity.oauth2Client()HttpSecurity.oauth2Client().authorizationCodeGrant()spring-doc.cn

以下代码显示了 DSL 提供的完整配置选项:HttpSecurity.oauth2Client()spring-doc.cn

OAuth2 客户端配置选项
@Configuration
@EnableWebSecurity
public class OAuth2ClientSecurityConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.oauth2Client(oauth2 -> oauth2
				.clientRegistrationRepository(this.clientRegistrationRepository())
				.authorizedClientRepository(this.authorizedClientRepository())
				.authorizedClientService(this.authorizedClientService())
				.authorizationCodeGrant(codeGrant -> codeGrant
					.authorizationRequestRepository(this.authorizationRequestRepository())
					.authorizationRequestResolver(this.authorizationRequestResolver())
					.accessTokenResponseClient(this.accessTokenResponseClient())
				)
			);
		return http.build();
	}
}
@Configuration
@EnableWebSecurity
class OAuth2ClientSecurityConfig {

    @Bean
    open fun filterChain(http: HttpSecurity): SecurityFilterChain {
        http {
            oauth2Client {
                clientRegistrationRepository = clientRegistrationRepository()
                authorizedClientRepository = authorizedClientRepository()
                authorizedClientService = authorizedClientService()
                authorizationCodeGrant {
                    authorizationRequestRepository = authorizationRequestRepository()
                    authorizationRequestResolver = authorizationRequestResolver()
                    accessTokenResponseClient = accessTokenResponseClient()
                }
            }
        }
        return http.build()
    }
}

除了 DSL 之外,还支持 XML 配置。HttpSecurity.oauth2Client()spring-doc.cn

以下代码显示了 security 命名空间中可用的完整配置选项:spring-doc.cn

OAuth2 客户端 XML 配置选项
<http>
	<oauth2-client client-registration-repository-ref="clientRegistrationRepository"
				   authorized-client-repository-ref="authorizedClientRepository"
				   authorized-client-service-ref="authorizedClientService">
		<authorization-code-grant
				authorization-request-repository-ref="authorizationRequestRepository"
				authorization-request-resolver-ref="authorizationRequestResolver"
				access-token-response-client-ref="accessTokenResponseClient"/>
	</oauth2-client>
</http>

负责与一个或多个 () 协作管理 OAuth 2.0 客户端的授权(或重新授权)。OAuth2AuthorizedClientManagerOAuth2AuthorizedClientProviderspring-doc.cn

以下代码显示了如何注册 an 并将其与支持 、 、 和 authorization 授权类型的复合关联的示例:OAuth2AuthorizedClientManager@BeanOAuth2AuthorizedClientProviderauthorization_coderefresh_tokenclient_credentialspasswordspring-doc.cn

@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
		ClientRegistrationRepository clientRegistrationRepository,
		OAuth2AuthorizedClientRepository authorizedClientRepository) {

	OAuth2AuthorizedClientProvider authorizedClientProvider =
			OAuth2AuthorizedClientProviderBuilder.builder()
					.authorizationCode()
					.refreshToken()
					.clientCredentials()
					.password()
					.build();

	DefaultOAuth2AuthorizedClientManager authorizedClientManager =
			new DefaultOAuth2AuthorizedClientManager(
					clientRegistrationRepository, authorizedClientRepository);
	authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

	return authorizedClientManager;
}
@Bean
fun authorizedClientManager(
        clientRegistrationRepository: ClientRegistrationRepository,
        authorizedClientRepository: OAuth2AuthorizedClientRepository): OAuth2AuthorizedClientManager {
    val authorizedClientProvider: OAuth2AuthorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
            .authorizationCode()
            .refreshToken()
            .clientCredentials()
            .password()
            .build()
    val authorizedClientManager = DefaultOAuth2AuthorizedClientManager(
            clientRegistrationRepository, authorizedClientRepository)
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider)
    return authorizedClientManager
}