OAuth Error error invalid_request error_description At least one redirect_uri must be registered

出现该错误:
OAuth Error error=“invalid_request”, error_description="At least one redirect_uri must be registered

解决方法:
在OAuth服务端设置中添加 redirectUris(String…)方法,如

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //添加客户端信息
        clients.inMemory()                  // 使用in-memory存储客户端信息
                .withClient("client")       // client_id
                .redirectUris("http://www.baidu.com");
    }
}
OAuth2出现 `error="invalid_grant"` 和 `error_description="Invalid redirect"` 错误,通常意味着重定向URI不符合注册值或者使用了不恰当的授权类型。以下是一些可能的解决方案: ### 检查重定向URI的配置 确保在OAuth2客户端注册时,重定向URI被正确配置。在Spring OAuth2中,若使用 `AuthorizationServerConfigurerAdapter` 进行配置,要保证客户端注册的重定向URI与实际请求的重定向URI一致。例如: ```java @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("yourClientId") .secret("yourClientSecret") .authorizedGrantTypes("authorization_code") .scopes("read", "write") .redirectUris("http://your-valid-redirect-uri.com"); } ``` ### 确认授权类型 重定向URI通常仅能用于隐式(implicit)或授权码(authorization_code)授权类型。要确保请求使用了正确的授权类型。比如,使用授权码授权类型时,请求的URL应类似如下格式: ``` https://authorization-server.com/oauth/authorize? response_type=code& client_id=yourClientId& redirect_uri=http://your-valid-redirect-uri.com& scope=read write ``` ### 检查重定向URI的合法性 保证重定向URI是合法的URL,并且没有多余的字符或者格式错误。像 `Invalid redirect: =http://www.baidu.com` 这样的错误,可能是因为重定向URI中存在多余的字符。 ### 检查服务器端配置 确认服务器端的 `DefaultRedirectResolver.java` 中的校验逻辑是否正确。要确保该校验逻辑能够正确匹配重定向URI。 ### 调试和日志记录 在开发环境中,可以开启详细的日志记录,从而查看OAuth2请求和响应的详细信息。例如,在Spring Boot中,可以在 `application.properties` 或 `application.yml` 中添加如下配置: ```properties logging.level.org.springframework.security.oauth2=DEBUG ``` 通过以上步骤,应该能够解决 `error="invalid_grant"` 和 `error_description="Invalid redirect"` 错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值