【问题标题】:Spring security oauth2 client - too many redirectSpring security oauth2 客户端 - 重定向太多
【发布时间】:2020-10-04 20:52:13
【问题描述】:

这是我的应用程序属性文件:

spring.security.oauth2.client.registration.myclient.client-id=SampleClientId
spring.security.oauth2.client.registration.myclient.client-secret=secret
spring.security.oauth2.client.provider.myclient.authorization-uri=http://localhost:8081/auth/oauth/authorize
spring.security.oauth2.client.provider.myclient.token-uri=http://localhost:8081/auth/oauth/token
spring.security.oauth2.client.provider.myclient.user-info-uri=http://localhost:8081/auth/user/me
spring.security.oauth2.client.registration.myclient.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.myclient.redirect-uri=http://localhost:8080/hellouser
spring.security.oauth2.client.provider.myclient.userNameAttribute=user

这是我的配置类:

@Configuration class SecurityConfig extends WebSecurityConfigurerAdapter {

  @throws[Exception]
  override protected def configure(http: HttpSecurity): Unit = {
    http.authorizeRequests.anyRequest.authenticated.and.oauth2Login
      .redirectionEndpoint()
      .baseUri("/")
  }
}

这是我的控制器:

case class Message(val string: String)

@Controller
class SuccessController {

  @GetMapping(path = Array("/hellouser"), produces = Array(MediaType.APPLICATION_JSON_VALUE))
  def getHelloUser(model: Model, authentication: OAuth2AuthenticationToken ): Message = {
    Message("hello user")
  }
}

当我登录时,我会返回 ERR_TOO_MANY_REDIRECTS

在开发者控制台的网络部分,我看到这三个调用无限重复:

http://localhost:8081/auth/oauth/authorize?response_type=code&client_id=SampleClientId&state=xyz&redirect_uri=http://localhost:8080/hellouser

http://localhost:8080/hellouser?code=abc&state=xyz

http://localhost:8080/oauth2/authorization/myclient

我做错了什么?

谢谢

【问题讨论】:

标签: java scala spring-security spring-security-oauth2


【解决方案1】:

不要将您的重定向 URI 设置为 API。

授权代码流中的重定向 URI 用于获取授权代码,然后可以将其交换为令牌。

你的设置中发生了什么

  1. 请求开始
  2. 用户未登录
  3. 重定向到授权登录
  4. 从 IdP 重定向回预定的 URI
  5. 请求 API /helloworld
  6. 转到第 2 步。

https://auth0.com/docs/flows/concepts/auth-code 看到了这一点,您没有进入第 4 步,而是将流程短路到第 1 步

您的配置使身份验证代码流永远不会完成。您应该只保留 Spring 默认重定向 URI 并删除 spring.security.oauth2.client.registration.myclient.redirect-uri=http://localhost:8080/hellouser

您也不需要设置redirectionEndpoint#baseUri 配置。

编辑,

另外说明的是,使用 Spring Security,第 1 步的初始请求被保存,当授权代码流在第 4 步完成时,Spring 将自动重播保存的请求。

【讨论】:

    猜你喜欢
    • 2012-10-22
    • 2013-06-20
    • 2022-11-08
    • 1970-01-01
    • 2019-04-06
    • 2017-09-17
    • 2020-02-11
    • 2021-03-07
    • 2019-07-20
    相关资源
    最近更新 更多