【问题标题】:spring-social-google not sending clientId/clientKeyspring-social-google 不发送 clientId/clientKey
【发布时间】:2012-10-27 03:51:35
【问题描述】:

我正在尝试向现有的 Spring webapp 添加 OAuth 支持,以允许人们使用他们的 Google/Facebook/Twitter/等登录。帐户。为此,我使用了“spring-social”框架和“spring-social-google”库。我也在尝试在一些限制条件下执行此操作:

  • 现有的 webapp 不使用 spring-security 进行身份验证或控制对资源的访问,它提供了自己的基于表单的身份验证。
  • 现有的 web 应用程序不使用 servlet 容器的 Principal 跟踪经过身份验证的用户详细信息,而是在 HTTP 会话中存储对经过身份验证的用户的引用。
  • 现有的 webapp 没有(也不能)将 Spring DispatcherServlet 绑定到 webapp 根 URL(即/)。

数据库中的帐户由电子邮件唯一键入,所以我真正想做的就是将大致如下的流程粘合在一起:

Login Page (user chooses between OAuth and direct login)
    -> <Provider OAuth Flow>  (user completes OAuth authorization)
        -> OAuth Callback URL (grab user email, check for existing account, create if needed) 
            -> Post-login Landing Page (done)

无论如何,上面提到的限制导致了各种各样的问题,其中大部分我已经设法找到解决方法。但是,我从 Google OAuth 连接器中得到了一些奇怪的行为(Twitter 和 Facebook 似乎工作正常)。基本上,在对 Google 的最终请求期间,它似乎没有发送 OAuth clientIdclientSecret

DEBUG: org.springframework.web.client.RestTemplate - Writing [
    {code=[4/dVVrFDpNLDGTmXCuuQj6fcfaetEU.UkLPQd7NOLYbgrKXntQAax0INYiydQI], 
     redirect_uri=[http://localhost:8080/oauth/signin/google], 
     grant_type=[authorization_code]}] as "application/x-www-form-urlencoded"

这将返回代码 400(“错误请求”)。

如果我前往 hurl.it 并将相同的数据发布到相同的 URL (https://accounts.google.com/o/oauth2/token) 并手动添加 client_idclient_secret 值,调用返回成功响应。

那么是什么原因导致 Google 连接器忽略了这些值?

作为参考,我在我的项目中包含了 spring-social-google 库,例如:

    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-google</artifactId>
        <version>1.0.0.M1</version>
    </dependency>

...然后在我的@Configuration 课程中我得到了:

@Bean
@Scope(value="singleton", proxyMode=ScopedProxyMode.INTERFACES) 
public ConnectionFactoryLocator connectionFactoryLocator() {
    ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();

    registry.addConnectionFactory(
            new GoogleConnectionFactory(
                    "<google client id>",  
                    "<google secret key>"));

    registry.addConnectionFactory(
            new FacebookConnectionFactory(
                    "<facebook client id>", 
                    "<facebook secret key>"));

    registry.addConnectionFactory(
            new TwitterConnectionFactory(
                    "<twitter client id>",   
                    "<twitter secret key>"));

    return registry;
}

我使用的其余部分几乎是直接来自spring-social-showcase 示例的标准(尽管为了删除无关的东西并在上述限制内工作)。奇怪的是,尝试使用 Google 登录确实正确显示了 Google 上的 OAuth 授权页面,并且我的应用程序/项目名称正确显示。在我点击“允许”按钮授权 OAuth 登录并返回 web 应用程序后发生错误。

无论如何,什么可能导致此问题,以及如何解决?

【问题讨论】:

    标签: java spring oauth


    【解决方案1】:

    您可能没有在 google 开发者控制台中打开 google+ api。

    这是与获取 OAuth 2 令牌或 api 密钥不同的任务。

    如果你还没有这样做

    1. http://console.developers.google.com

    2. 导航到您的项目。

    3. 单击“API”。

    4. 检查“Google+ API”是否在已启用 API 的列表中。如果不是,请浏览并启用它。

    【讨论】:

      猜你喜欢
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      相关资源
      最近更新 更多