java - 谷歌的 Spring Boot oauth 回调

标签 java spring-boot openid-connect google-oauth

我正在分享有关我的问题的完整代码。 我正在获取通过 Social-cfg.xml 处理的社交配置文件,它位于类路径中。

social-cfg.xml

google.client.id=#####################################################################
google.client.secret=############################
google.scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile

我已经制作了java处理程序来处理这个配置文件

socialConfig.java

    package com.inno.config;

    import javax.sql.DataSource;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;
    import org.springframework.security.crypto.encrypt.Encryptors;
    import org.springframework.social.UserIdSource;
    import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
    import org.springframework.social.config.annotation.EnableSocial;
    import org.springframework.social.config.annotation.SocialConfigurer;
    import org.springframework.social.connect.ConnectionFactoryLocator;
    import org.springframework.social.connect.ConnectionRepository;
    import org.springframework.social.connect.ConnectionSignUp;
    import org.springframework.social.connect.UsersConnectionRepository;
    import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
    import org.springframework.social.connect.web.ConnectController;
    import org.springframework.social.google.connect.GoogleConnectionFactory;
    import org.springframework.social.security.AuthenticationNameUserIdSource;

    import com.inno.dao.AppUserDAO;
    import com.inno.service.ConnectionSignUpImpl;
    @Configuration
    @EnableSocial
    // Load to Environment.
    @PropertySource("classpath:social-cfg.properties")
    public class SocialConfig implements SocialConfigurer {


         private boolean autoSignUp = false;

          @Autowired
            private DataSource dataSource;
          @Autowired
            private AppUserDAO appUserDAO;
        @Override
        public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
             try {
                    this.autoSignUp = Boolean.parseBoolean(env.getProperty("social.auto-signup"));
                } catch (Exception e) {
                    this.autoSignUp = false;
                }
            // Google
                GoogleConnectionFactory gfactory = new GoogleConnectionFactory(//
                        env.getProperty("google.client.id"), //
                        env.getProperty("google.client.secret"));

                gfactory.setScope(env.getProperty("google.scope"));

                cfConfig.addConnectionFactory(gfactory);
            }
 //............. more code regards to another functionality.

当我点击这个href时

<a th:href="@{/auth/google}">Google</a>
        <br />

然后它重定向到谷歌页面,错误如下

Error: redirect_uri_mismatch

The redirect URI in the request, http://localhost:8787/auth/google, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/#######?project=#######

当我访问https://console.developers.google.com/apis/credentials/oauthclient/#######?project=#######

有两个选项

  1. 授权的 JavaScript 来源
  2. 授权重定向 URI

我已将它们设置为使用我的网络应用程序进行验证。我只是不知道这个问题的重点在哪里。请帮帮我。如果需要,还可以询问任何其他要求。

最佳答案

Google 开发者控制台中的重定向 URI 必须与您发送请求的位置完全匹配。在您的情况下,错误消息会准确告诉您将请求表发送到的位置。

http://localhost:8787/auth/google

这意味着您需要在 Google 开发者控制台的“授权重定向 URI”下准确输入该信息,否则身份验证服务器将不会接受您的请求。

注意 必须包含端口。 Exactly 意味着除了 cased 之外完全一样。

关于java - 谷歌的 Spring Boot oauth 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50247032/

相关文章:

java - 邻接矩阵 -> 有向图 -> DFS

java - 对以 UTF 格式编写的文件执行二分查找

Java 媒体框架 MP3 插件

java数据类型到字节数组

spring-boot - Spring Security : what function does AuthenticationManager. authenticate() 执行

spring-boot - 如何在 Spring Boot 应用程序中创建第二个 RedisTemplate 实例

java - 将属性传递给 Maven 子模块,该子模块没有当前模块作为父模块

c# - 用户信息端点未找到 openid 范围

moodle - 无法使用 Moodle 的 OIDC 插件使用 Keycloak 登录

oauth-2.0 - 是否应该将身份验证代码将访问 token 交换访问权转移到用于Web API/SPA OpenID Connect实现的API?