java - Spring security oauth2 客户端 - 重定向太多

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

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

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("/")
  }
}

这是我的 Controller :

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

我做错了什么?

谢谢

最佳答案

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

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

您的设置中发生的事情是

  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 将自动重放保存的请求。

关于java - Spring security oauth2 客户端 - 重定向太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62388299/

相关文章:

java - MongoDB - 更新文档列表中的对象

Java:使用操作系统标准应用程序打开 jar 中的资源(txt 文件)

java - 通过方法返回对象 Bin 时遇到问题。需要 "Cast"或更改为对象

scala - 使用ScalaTest时,从SBT中排除带有特定标签的测试

java - 过滤器模式定义不适用

java - 如何在RMI中自动将接口(interface).class文件导入到客户端?

scala - 如何在 Akka-Stream 2.0 流程开始时向 ActorRef 发送消息?

java - Spark 历史日志手动解压

java - 如何在 MockMvc 测试期间使用正确的 Spring Security 配置

Grails oAuth2 Provider 插件无法对 grant_type 'password' 进行身份验证