Spring Social:"无法获得ConnectionRepository:没有用户登录"

时间:2015-12-29 13:37:15

标签: spring facebook spring-security spring-social spring-social-facebook

我尝试使用Facebook登录,如中所述 https://github.com/spring-guides/gs-accessing-facebook

当我尝试创建JdbcUsersConnectionRepository时,我需要在类路径中使用spring security。 当我添加spring security时,我会收到

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

尝试接收连接时

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

或检查

if (!facebook.isAuthorized())

所有这些只有在Spring安全性位于类路径

时才会发生

2 个答案:

答案 0 :(得分: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

  

在内部,Spring Social的配置支持将使用UsersConnectionRepository创建一个请求范围的ConnectionRepository bean。在这样做时,它必须识别当前用户。因此,我们还必须覆盖getUserIdSource()以返回UserIdSource的实例。

     

在这种情况下,我们将返回AuthenticationNameUserIdSource的实例。 UserIdSource接口的此实现假定应用程序受Spring Security保护。它使用SecurityContextHolder查找SecurityContext,并从中返回Authentication对象的name属性。

     

如果您的应用程序不受Spring Security保护,您需要将UserIdSource接口实现为适合您应用程序的安全机制。 UserIdSource界面如下所示:

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

getUserId()方法只返回一个唯一标识当前用户的String。

更多信息here

答案 1 :(得分:-1)

如果您使用的是Spring引导,则不应将属性安全基础设置为false。在application.properties中隐藏此行。这会禁用安全性,如果禁用spring-security,则引导会引发错误。

#security.basic.enabled=false