You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot Maven项目Google OAuth2报错400: redirect_uri_mismatch求助

Google OAuth2 redirect_uri_mismatch错误解决方案

1. 修正配置文件语法错误

你的application.properties里spring.security.oauth2..client多了一个小数点,这会导致Spring无法正确识别OAuth2客户端配置项。修正后配置如下:

spring.security.oauth2.client.registration.google.client-id=792979004767-5fvs24lfomcue1h532sqdluskt60ieql.apps.googleusercontent.com
spring.security.oauth2.client.registration.google.client-secret={Client_Secret}
# 显式指定重定向URI,确保和Google Cloud Console配置完全一致
spring.security.oauth2.client.registration.google.redirect-uri=http://localhost:8080/login/oauth2/code/google

2. 核对Google Cloud Console的重定向URI

  • 进入Google Cloud Console的API和服务 > 凭据页面,找到你的OAuth 2.0客户端ID并编辑
  • 在已授权的重定向URI列表中,确认http://localhost:8080/login/oauth2/code/google已添加,且完全匹配:
    • 区分http/https(本地测试用http即可)
    • 端口号、路径不能有任何拼写错误,不能多/少斜杠

3. 检查Spring Security配置

确保你的安全配置类正确启用OAuth2登录,示例代码:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .oauth2Login();
    }
}

4. 清理缓存与授权记录

  • 清除浏览器的Cookie和缓存,避免旧会话干扰
  • 访问Google账号权限管理页面,移除对该应用的授权后重新尝试登录

5. 确认客户端ID匹配

确保application.properties中的client-id和Google Cloud Console里对应客户端ID完全一致,不要混用不同项目的凭据。

内容的提问来源于stack exchange,提问作者Pratyush Bindal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:15:09