【发布时间】:2020-07-04 10:02:04
【问题描述】:
我的 Spring Boot 客户端应用程序如何访问由例如提供的刷新令牌。谷歌在 Spring Security 5 中?
很简单的问题。远程授权服务器(例如 Google)发送一个刷新令牌,我想使用它。在 Spring Security 5 中持久化和检索它的最佳方式是什么?
似乎 this answer、this question 和 this exernal link 描述了一种不再兼容的方法,因为 Oauth2 在 Spring Security 5 中成为一等公民。
上下文:
刷新令牌允许客户端应用程序在用户会话过期后继续访问资源。 Per Google's docs,刷新令牌应该是持久的:
应用程序应存储刷新令牌以供将来使用,并使用访问令牌访问 Google API。
Spring 安全性使 访问令牌 以 OAuth2AuthenticationToken 的形式广泛使用,但其中不包含刷新令牌。
刷新令牌在OidcUserService(或覆盖它的类)中也不可用,因为public OidcUser loadUser(OidcUserRequest userRequest) 无权访问刷新令牌。这很糟糕,因为使用自定义类覆盖 OidcUserService 会很好,该类可以从 OIDC 用户详细信息中创建/检索用户并同时保存其关联的刷新令牌。
OAuth2LoginAuthenticationFilter 将刷新令牌保存在 ClientRegistrationRepository 中:
OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(
authenticationResult.getClientRegistration(),
oauth2Authentication.getName(),
authenticationResult.getAccessToken(),
authenticationResult.getRefreshToken());
this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, oauth2Authentication, request, response);
默认实现将令牌保存在临时内存中,这不适合分布式应用程序或在重启后持续存在。
似乎有一个JdbcOauth2AuthorizedClientService、docs recently added 和一个schema that suggests 可能有用,但没有提供配置它或使用它检索刷新令牌的示例。
那么客户端应用程序如何在 Spring Security 5 中持久化和访问刷新令牌?
【问题讨论】:
标签: spring-security oauth-2.0 spring-security-oauth2 refresh-token