2012-11-07 15 views
3

我試圖添加OAuth支持到現有的Spring Web應用程序,以允許用戶使用他們的Google/Facebook/Twitter /等登錄。賬戶。爲此,我使用'春季社交'框架和'春季 - 社交 - 谷歌'庫。我也想做到這一點,而一些限制的範圍內工作:spring-social-google不發送clientId/clientKey

  • 現有的Web應用程序不使用彈簧安全認證或用於控制對資源的訪問,它提供了自己的基於表單的認證。
  • 現有webapp不使用servlet容器的Principal跟蹤經過驗證的用戶詳細信息,而是在HTTP會話中存儲對已驗證用戶的引用。
  • 現有webapp沒有(也不能)將Spring DispatcherServlet綁定到webapp根URL(即/)。

帳戶在數據庫中唯一通過e-mail鍵,所以真的所有我試圖做的是膠在一起去大致是這樣一個流程:

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似乎正常工作)。基本上,看來,它不是最終的請求期間發送的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和POST相同的數據到相同的URL(https://accounts.google.com/o/oauth2/token)和client_idclient_secret值手動添加,調用返回一個成功的迴應。

那麼,什麼可能導致谷歌連接器忽略這些值?

僅供參考,我包括在我的項目,如春天 - 社會 - 谷歌圖書館:

<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正確顯示了Google上的OAuth授權頁面,並正確顯示了我的應用/項目名稱。我點擊「允許」按鈕來授權OAuth登錄並返回到Web應用程序後,就會發生錯誤。

無論如何,有什麼可能導致這個問題,以及它如何解決?

回答

1

您可能沒有在google開發者控制檯中關閉google + api。

這是獲取OAuth 2令牌或api密鑰的單獨任務。

如果您還沒有這樣做的話

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

  2. 瀏覽到您的項目。

  3. 點擊'API'。

  4. 檢查「Google+ API」是否位於已啓用API的列表中。如果不是,請瀏覽並啓用它。