我有像 Okta 这样的外部身份验证服务器,它验证颁发的凭据并返回代码。 接下来,我有 Spring Boot 应用程序,它公开了简单的 API,我想通过使用授权代码流来保护它。 Spring Boot 依赖项是:spring-boot-starter-oauth2-client 和 spring-boot-starter-oauth2-resource-server。 我想要实现的主要流程是:
问题从第3点开始。 我点击了端点,它重定向我使用 Auth Server 网页登录。 成功登录后 - 它返回 http://localhost:8080/test?code=asdawq34sdaw&state=dqwd453453 但接下来它会自动将其重定向到:http://localhost:8080/login/oauth2/callback/myapp?code=asdawq34sdawn&state=dqwd453453。 最后我得到了 ERR_TO_MANY_REDIRECTS 以及上述所有请求的顺序。
我只在application.yml中设置了一些属性:
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: <issuer_uri>
client:
registration:
myapp:
client-id: client_id
redirect-uri: https://localhost:8080/login/oauth2/callback/myapp
authorization-grant-type: authorization_code
scope:
- test_scope
provider:
myapp:
issuer-uri: <issuer_uri>
公开简单的 API,我想通过使用授权代码流来保护它
REST API 资源服务器和处理 OAuth2 流是客户端的责任。用于获取访问令牌的流程与资源服务器业务无关。资源服务器只关心请求是否获得了有效访问令牌的授权,以及它是否应该根据令牌声明(在令牌内部或从中进行内省)授予对所请求资源的访问权限
使用
spring-boot-starter-oauth2-resource-server
(不是spring-boot-starter-oauth2-client
)配置您的API并删除登录。
授权码流的触发由客户端完成。客户样本:
spring-cloud-gateway
配置为 Backend For Frontend