Spring security 授权代码流很多重定向

问题描述 投票:0回答:1

我有像 Okta 这样的外部身份验证服务器,它验证颁发的凭据并返回代码。 接下来,我有 Spring Boot 应用程序,它公开了简单的 API,我想通过使用授权代码流来保护它。 Spring Boot 依赖项是:spring-boot-starter-oauth2-client 和 spring-boot-starter-oauth2-resource-server。 我想要实现的主要流程是:

  1. 输入受保护的端点:http://localhost:8080/test,如果用户未通过身份验证且未获得授权,则将其重定向到身份验证服务器登录页面。
  2. 正确的凭据后,URL 中返回代码
  3. 我没有看到自动向身份验证服务器请求访问令牌

问题从第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>
java spring security oauth-2.0
1个回答
0
投票

公开简单的 API,我想通过使用授权代码流来保护它

REST API 资源服务器和处理 OAuth2 流是客户端的责任。用于获取访问令牌的流程与资源服务器业务无关。资源服务器只关心请求是否获得了有效访问令牌的授权,以及它是否应该根据令牌声明(在令牌内部或从中进行内省)授予对所请求资源的访问权限

使用

spring-boot-starter-oauth2-resource-server
(不是
spring-boot-starter-oauth2-client
)配置您的API并删除登录。

授权码流的触发由客户端完成。客户样本:

  • Postman(用于测试您的 API)
  • OAuth2 JS 框架的客户端库(angular-auth-oidc-client 是 Angular 的示例)
  • spring-cloud-gateway
    配置为 Backend For Frontend
        
© www.soinside.com 2019 - 2024. All rights reserved.