2011-06-20 44 views
4

我有一個使用用戶名/密碼SpringSecurity配置的可用Web應用程序。現在我想將它移植到一個簡單的Facebook應用程序中。出於某種原因,我想通過使用返回的Facebook訪問令牌來進行身份驗證,並保留用戶名 - 密碼驗證器。在FacebookApp中將Facebook身份驗證集成到Spring Security中

在細節,我會檢查認證的用戶的Facebook訪問令牌,通過返回:

https://graph.facebook.com/oauth/access_token?client_id=[my_api_key]&redirect_uri=[my_redirect_uri]&client_secret=[my_api_secret]&code=[code]

用戶不需要提供任何的用戶名/密碼,因爲他們已經登錄與Facebook。但我想保留(用戶名/密碼)彈簧安全配置,以便用戶可以登錄我的原始網站。

SpringSecurity支持這種認證嗎?如果答案是肯定的,我想知道如何做到這一點?我是否需要編寫自定義身份驗證提供程序來執行此操作?

UPDATE:最後,我們有定製的方式SpringSecurity認證,使其接受的access_token作爲驗證參數通過擴展UsernamePasswordAuthenticationFilter(聲明爲formLoginFilter

+0

@HaveAGuess:我已經更新了我的問題的答案。 –

回答

9

有一個從春天另一個項目:Spring Social這非常有用。

它支持多個社交網絡。我成功地用它來驗證Facebook。然後我寫了一個小功能,Facebook的用戶登錄到我的春季安全方面:

protected void authenticate(UserDTO user){ 
    SecurityContextHolder.getContext().getAuthentication(); 
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()); 
    token.setDetails(new WebAuthenticationDetails(getRequest())); 
    Authentication authentication = authenticationManager.authenticate(token); 
    SecurityContextHolder.getContext().setAuthentication(authentication); 
} 

UserDTO需要有一個用戶名和(生成)的密碼屬性和需要保存在數據庫中,以便您的user-service(從春季安全)可以檢索它。

+0

Bart Vangeneugden它不工作...顯示錯誤..我也包括春天的API ..是否有任何API也是要添加 – lulu

相關問題