我正在将 Spring Cloud API Gateway 与 OAuth2 结合使用,并且在尝试获取自定义范围的访问令牌时遇到问题。我的 OAuth2 提供商是 Microsoft。
我需要访问令牌来针对我的自定义 API 进行身份验证,这需要自定义范围。但是,我注意到 /userinfo 端点也需要被调用,并且它需要一个 openid 范围。
如果我同时包含 openid 和自定义范围,请求将被拒绝。如果我排除 openid,则对 /userinfo 的调用将因“范围不足”而失败。
那么,如何在不影响 /userinfo 端点调用的情况下获取自定义范围 API 的访问令牌?
# Spring Cloud API Gateway OAuth2 client settings
spring:
security:
oauth2:
client:
registration:
my-client:
provider: microsoft
client-id: xxx
client-secret: xxx
authorization-grant-type: authorization_code
redirect-uri: http://localhost:8080/login/oauth2/code/your-client
scope: custom-scope # Can't add 'openid' here without the request failing
这是 Azure AD 的一个特点。最好的选择是让客户仅使用自定义范围。然后通过 API 获取用户信息,该 API 使用
On Behalf Of Flow
将访问令牌交换为有权调用 Graph API 的用户。有关更多信息,请参阅我的这些资源: