Spring 安全 oauth2 客户端 - 重定向太多
Spring security oauth2 client - too many redirect
这是我的应用程序 属性 文件:
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
这是我的配置class:
@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
在开发者控制台的网络部分,我看到这三个调用是无限重复的:
我做错了什么?
谢谢
不要将重定向 URI 设置为 API。
授权代码流中的重定向 URI 用于获取授权代码,然后可以用它交换令牌。
您的设置中发生的事情是
- 请求开始
- 用户未登录
- 重定向到授权登录
- 从 IdP 重定向回预定的 URI
- 请求 API
/helloworld
- 转到第 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 安全性会保存第 1 步的初始请求,当授权代码流程在第 4 步完成时,Spring 将自动重播已保存的请求。
这是我的应用程序 属性 文件:
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
这是我的配置class:
@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
在开发者控制台的网络部分,我看到这三个调用是无限重复的:
我做错了什么?
谢谢
不要将重定向 URI 设置为 API。
授权代码流中的重定向 URI 用于获取授权代码,然后可以用它交换令牌。
您的设置中发生的事情是
- 请求开始
- 用户未登录
- 重定向到授权登录
- 从 IdP 重定向回预定的 URI
- 请求 API
/helloworld
- 转到第 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 安全性会保存第 1 步的初始请求,当授权代码流程在第 4 步完成时,Spring 将自动重播已保存的请求。