6

我想用在 https://github.com/spring-guides/gs-accessing-facebook春社會:「無法獲取ConnectionRepository:沒有用戶登錄」

當我試圖創建JdbcUsersConnectionRepository描述的Facebook登錄,我需要在春季安全班級路徑。 當我加春天的安全我收到

"java.lang.IllegalStateException: Unable to get a ConnectionRepository: no user signed in" 

嘗試接收連接

Connection<Facebook> connection = connectionRepository.findPrimaryConnection(Facebook.class); 

或檢查時

if (!facebook.isAuthorized()) 

,當春天的安全性是在類路徑

這一切只發生

回答

1

社交連接應該對應任何認證使用河看來用戶應該通過用戶名/密碼或服務提供商登錄。嘗試一下http://docs.spring.io/spring-social/docs/1.0.x/reference/html/signin.html

"java.lang.IllegalStateException: Unable to get a ConnectionRepository: no user signed in" 

它發生,因爲默認情況

使用AuthenticationNameUserIdSource內部,春節社會的配置支持,將使用UsersConnectionRepository創建請求範圍ConnectionRepository豆。這樣做時,它必須識別當前用戶。因此,我們還必須重寫getUserIdSource()以返回UserIdSource的實例。

在這種情況下,我們返回AuthenticationNameUserIdSource的一個實例。 UserIdSource接口的這種實現假定應用程序使用Spring Security進行保護。它使用SecurityContextHolder查找SecurityContext,並從中返回Authentication對象的name屬性。

如果您的應用程序沒有使用Spring Security進行保護,您需要實現UserIdSource接口作爲應用程序安全機制的適用範圍。該UserIdSource界面看起來像這樣:

package org.springframework.social; 
public interface UserIdSource { 
    String getUserId(); 
} 

的getUserId()方法簡單地返回唯一標識當前用戶的字符串。

更多信息here

+0

你能指點我一個Facebook的UserIdSource實現的例子嗎? 爲了返回用戶ID,我需要知道用戶是否連接。我不能這樣做,因爲我不能撥打 connectionRepository.findPrimaryConnection(Facebook.class) –

+0

@PavelBernshtam您好!爲什麼你的用戶沒有登錄,你試圖獲得他的社會聯繫(SecurityContextHolder.getContext()。getAuthentication()爲空)?社交連接應該對應於任何用戶。看來用戶應該通過用戶名/密碼或服務提供商登錄。你嘗試使用http://docs.spring。IO /彈簧社會/文檔/ 1.0.x系列/參考/ HTML/signin.html? – vitaly256

-1

如果您在使用Spring啓動,那麼財產安全的基本不應該被設置爲false。在application.properties中隱藏此行。這會禁用安全性,如果禁用了spring-security,則會引發錯誤。

#security.basic.enabled=false