【发布时间】:2020-07-20 15:28:54
【问题描述】:
我正在尝试使用 SpringBoot 1.5.13 和 spring-security-oauth2 2.0.15.RELEASE 实现 OAuth2 客户端。
我希望能够通过 auth_code 授权授权匿名用户。
但是,我在尝试完成身份验证流程时遇到了问题。
因为我不想调用任何其他外部服务来获取用户身份信息,所以我使用DefaultTokenServices。 DefaultTokenServices 需要一个 TokenStore 才能运行,所以我提供了最简单的一个 - InMemoryTokenStore。
我尝试了两种方法都失败了:
1。重定向到 OAuth2ClientAuthenticationProcessingFilter
如果我在授权授予后将用户重定向到一个 URL,OAuth2ClientAuthenticationProcessingFilter 过滤,过滤器会正确请求并获取访问令牌。但是,它尝试做的下一件事是:
OAuth2Authentication result = tokenServices.loadAuthentication(accessToken.getValue());
该调用在BadCredentialsException("Could not obtain user details from token") 上失败,因为DefaultTokenService 尝试调用tokenStore#readAccessToken,但此时InMemoryTokenStore 完全为空。
2。重定向到另一个 URL(简单 RestController)
如果我在授权授予后将用户重定向到另一个 URL,这与 OAuth2ClientAuthenticationProcessingFilter 不匹配,我什至无法获得访问令牌。
尝试在 RestController 中调用 restTemplate.getAccessToken() 时,由于此异常,在 AccessTokenProviderChain 中调用失败:
if (auth instanceof AnonymousAuthenticationToken) {
if (!resource.isClientOnly()) {
throw new InsufficientAuthenticationException(
"Authentication is required to obtain an access token (anonymous not allowed)");
}
}
编辑:我尝试使用我自己的AccessTokenProviderChain 实现,它只是省略了这个检查,以及与clientTokenServices 相关的部分,一开始看起来它似乎工作。但我不确定这是否是一种足以满足多租户(多个匿名并发用户通过多个 OAuth2 提供者授权)的正确方法。
显然我的身份验证是匿名身份验证,我需要一个匿名身份验证流程!我知道在 Spring Security 5.2.x 中完全有可能,但我不明白如何在 spring-security-oauth2 中做到这一点?
auth_code grant 不能做匿名授权吗?
【问题讨论】:
标签: spring spring-boot spring-security oauth-2.0 spring-security-oauth2