Spring Security OAuth2 客户端凭据授权
概述
在没有明确的资源拥有者,或对于客户端来说资源拥有者不可区分,该怎么办?这是一种相当常见的场景。比如后端系统之间需要直接通信时,将使用客户端凭据授权。
OAuth2.0 文档描述客户端凭据授权:
客户端使用客户端凭据授予类型来获取用户上下文之外的访问令牌。这通常被客户端用来访问关于他们自己的资源,而不是访问用户的资源。
在本文中,您将了解使用 Spring Security 构建 OAuth2 客户端凭据授权,在没有经过身份验证的用户的情况下允许服务安全的相互操作。
OAuth2 客户端凭据授权相比于授权码授权更直接,它通常用于 CRON 任务和其他类型的后端数据处理等操作。
客户端凭据授予流程
当应用程序请求访问令牌以访问其自己的资源时,将使用客户端凭据授权,而不是代表用户。
请求参数
grant_type(必需)
该 grant_type 参数必须设置为 client_credentials。
scope(可选的)
您的服务可以支持客户端凭据授予的不同范围。
客户端身份验证(必需)
客户端需要对此请求进行身份验证。通常,该服务将允许附加请求参数 client_id 和 client_secret,或接受 HTTP Basic auth 标头中的客户端 ID 和机密。
OAuth2 授权服务器
这里我们使用 Spring Authorization Server 构建 OAuth2 授权服务器,具体详细细节我这里就不重复赘述,可以参考此文 JWT 与 Spring Security OAuth2 结合使用中授权服务器搭建,这里仅说明与之前授权码授予流程授权服务配置的不同之处。
配置
在我们使用 RegisteredClient 构建器类型创建一个客户端,将配置此客户端支持客户端凭据授权,并简单的将它存储在内存中。
@Beanpublic RegisteredClientRepository registeredClientRepository() { RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString()) .clientId("relive-client") .clientSecret("{noop}relive-client") .clientAuthenticationMethods(s -> { s.add(ClientAuthenticationMethod.CLIENT_SECRET_POST); s.add(ClientAuthenticationMethod.CLIENT_SECRET_BASIC); }) .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) .redirectUri("http://127.0.0.1:8070/login/oauth2/code/messaging-client-model") .scope("message.read") .clientSettings(ClientSettings.builder() .requireAuthorizationConsent(true) .requireProofKey(false) .build()) .tokenSettings(TokenSettings.builder() .accessTokenFormat(OAuth2TokenFormat.SELF_CONTAINED) .idTokenSignatureAlgorithm(SignatureAlgorithm.RS256) .accessTokenTimeToLive(Duration.ofSeconds(30 * 60)) .refreshTokenTimeToLive(Duration.ofSeconds(60 * 60)) .reuseRefreshTokens(true) .build()) .build();
return new InMemoryRegisteredClientRepository(registeredClient);}复制代码
复制代码
上述我们配置了一个 OAuth2 客户端,并将 authorizationGrantType 指定为 client_credentials:
使用 Spring Security 构建 OAuth2 资源服务器
OAuth2 资源服务器配置与此文 JWT 与 Spring Security OAuth2 结合使用中资源服务搭建一致,您可以参考此文中 OAuth2 资源服务介绍,或可以在文末中获取本文源码地址进行查看。
配置
OAuth2 资源服务器提供了一个/resource/article 受保护端点,并使用 Spring Security 保护此服务。
@BeanSecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.requestMatchers() .antMatchers("/resource/article") .and() .authorizeRequests() .mvcMatchers("/resource/article") .access("hasAuthority('SCOPE_message.read')") .and() .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); return http.build();}复制代码
复制代码
请注意,OAuth2 资源服务/resource/article 端点要求拥有“message.read”权限才可以访问,Spring 自动在所需范围名称前添加“SCOPE_”,这样实际所需的范围是“message.read”而不是“SCOPE_message.read”。
使用 Spring Security 构建 OAuth2 客户端
在本节中,您将使用当前推荐的 WebClient,WebClient 是 Spring 的 WebFlux 包的一部分。这是 Spring 的反应式、非阻塞 API,您可以在 Spring 文档中了解更多信息。
在此客户端中,在 @Scheduled 此注解定义的 CRON 任务下,您将使用 WebClient 来发出请求。
maven 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.7</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.6.7</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> <version>2.6.7</version></dependency><dependency> <groupId>org.springframework</groupId> <artifactId>spring-webflux</artifactId> <version>5.3.9</version></dependency><dependency> <groupId>io.projectreactor.netty</groupId> <artifactId>reactor-netty</artifactId> <version>1.0.9</version></dependency>复制代码
复制代码
配置
授权我们将在 application.yml 中配置 OAuth2 授权信息,并指定 OAuth2 客户端服务端口号:
server: port: 8070
spring: security: oauth2: client: registration: messaging-client-model: provider: client-provider client-id: relive-client client-secret: relive-client authorization-grant-type: client_credentials client-authentication-method: client_secret_post scope: message.read client-name: messaging-client-model provider: client-provider: token-uri: http://127.0.0.1:8080/oauth2/token复制代码
复制代码
接下来我们将创建一个 SecurityConfig 类用来配置 Spring Security OAuth2 客户端所需 Bean:
@BeanSecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeRequests(authorizeRequests -> authorizeRequests.anyRequest().permitAll() ) .oauth2Client(withDefaults()); return http.build();}
@BeanWebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) { ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager); return WebClient.builder() .filter(oauth2Client) .build();}
@BeanOAuth2AuthorizedClientManager authorizedClientManager(ClientRegistrationRepository clientRegistrationRepository, OAuth2AuthorizedClientService authorizedClientService) {
OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder .builder() .clientCredentials() .build(); AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrationRepository, authorizedClientService); authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
return authorizedClientManager;}复制代码
复制代码
我们创建一个 WebClient 实例用于向资源服务器执行 HTTP 请求,并给 WebClient 添加了一个 OAuth2 授权过滤器。AuthorizedClientServiceOAuth2AuthorizedClientManager 这是协调 OAuth2 客户端凭据授予请求的高级控制器类,这里我将指出 AuthorizedClientServiceOAuth2AuthorizedClientManager 是一个专门设计用于在 HttpServletRequest 的上下文之外使用的类。
来自 Spring 文档:
DefaultOAuth2AuthorizedClientManager 旨在用于 HttpServletRequest 的上下文中。在 HttpServletRequest 上下文之外操作时,请改用 AuthorizedClientServiceOAuth2AuthorizedClientManager。
接下来我们将创建使用 @Scheduled 注解定义的任务,并注入 WebClient 调用资源服务请求:
@Servicepublic class ArticleJob {
@Autowired private WebClient webClient;
@Scheduled(cron = "0/2 * * * * ? ") public void exchange() { List list = this.webClient .get() .uri("http://127.0.0.1:8090/resource/article") .attributes(clientRegistrationId("messaging-client-model")) .retrieve() .bodyToMono(List.class) .block(); log.info("调用资源服务器执行结果:" + list); }}
复制代码
复制代码
这个类中 exchange()方法使用 @Scheduled 注解每 2 秒触发一次请求,在我们启动所有服务后,你应该看到这样的输出:
2022-07-09 19:55:22.281 INFO 20305 --- [ scheduling-1] com.relive.ArticleJob : 调用资源服务器执行结果:[article1, article2, article3]2022-07-09 19:55:24.023 INFO 20305 --- [ scheduling-1] com.relive.ArticleJob : 调用资源服务器执行结果:[article1, article2, article3]2022-07-09 19:55:26.015 INFO 20305 --- [ scheduling-1] com.relive.ArticleJob : 调用资源服务器执行结果:[article1, article2, article3]2022-07-09 19:55:28.009 INFO 20305 --- [ scheduling-1] com.relive.ArticleJob : 调用资源服务器执行结果:[article1, article2, article3]复制代码
复制代码
结论
与往常一样,本文中使用的源代码可在 GitHub 上获得。
来源:https://juejin.cn/post/7133559411896746014
评论