我的 Spring Boot 客户端应用程序如何访问由例如提供的刷新令牌谷歌在 Spring Security 5 中?
很简单的问题。远程授权服务器(例如 Google)发送刷新令牌,我想使用它。在 Spring Security 5 中保存和检索它的最佳方法是什么?
它似乎这个答案 https://stackoverflow.com/questions/39779134/spring-oauth2-how-to-refresh-access-token, 这个问题 https://stackoverflow.com/questions/54513209/how-to-renew-access-token-with-the-refresh-token-in-oauth2-in-spring, and 这个外部链接 https://javadeveloperzone.com/spring-boot/spring-boot-oauth2-jdbc-token-store-example/描述自从 Oauth2 成为 Spring Security 5 中的一等公民以来不再兼容的方法。
Context:
刷新令牌允许客户端应用程序在用户会话过期后继续访问资源。根据谷歌的文档 https://developers.google.com/identity/protocols/oauth2#scenarios,刷新令牌应该是持久的:
应用程序应存储刷新令牌以供将来使用,并使用访问令牌来访问 Google API。
Spring Security 使得访问令牌以一种广泛可用的形式OAuth2身份验证令牌 https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/oauth2/client/authentication/OAuth2AuthenticationToken.html,但刷新令牌不包含在那里。
刷新令牌也不可用OidcUserService
(或覆盖它的类),因为public OidcUser loadUser(OidcUserRequest userRequest)
无权访问刷新令牌。这很糟糕,因为使用自定义类覆盖 OidcUserService 会很好,该类可以从 OIDC 用户详细信息中创建/检索用户并同时保存其关联的刷新令牌.
The OAuth2LoginAuthenticationFilter
将刷新令牌保存在 ClientRegistrationRepository 中:
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(
authenticationResult.getClientRegistration(),
oauth2Authentication.getName(),
authenticationResult.getAccessToken(),
authenticationResult.getRefreshToken());
this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, oauth2Authentication, request, response);
默认实现将令牌保留在临时内存中,这不适合分布式应用程序或在重新启动后保持不变。
似乎有一个JdbcOauth2AuthorizedClientService https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2Client-authorized-repo-service, with 最近添加的文档 https://github.com/spring-projects/spring-security/commit/b42f0f50ab57186c15c57cead4ebbda75be3cdc9, and a 暗示的图式 https://docs.spring.io/spring-security/site/docs/current/reference/html5/#dbschema-oauth2-client它可能有用,但没有提供配置它或使用它检索刷新令牌的示例。
那么客户端应用程序如何在 Spring Security 5 中持久保存并访问刷新令牌呢?