Spring 社交:"Unable to get a ConnectionRepository: no user signed in"

Spring Social: "Unable to get a ConnectionRepository: no user signed in"

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

当我尝试创建 JdbcUsersConnectionRepository 时,我需要 class 路径中的 spring 安全性。 当我添加 spring 安全性时,我收到

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

尝试接收连接时

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

或检查

if (!facebook.isAuthorized())

只有当 spring 安全性位于 class 路径中时,这一切才会发生

Social Connection 应对应于任何授权用户。看来用户应该通过 username/password 或服务提供商登录。试试看 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 默认使用

Internally, Spring Social’s configuration support will use the UsersConnectionRepository to create a request-scoped ConnectionRepository bean. In doing so, it must identify the current user. Therefore, we must also override the getUserIdSource() to return an instance of a UserIdSource.

In this case, we’re returning an instance of AuthenticationNameUserIdSource. This implementation of the UserIdSource interface assumes that the application is secured with Spring Security. It uses the SecurityContextHolder to lookup a SecurityContext, and from that return the name property of the Authentication object.

If your application isn’t secured with Spring Security, you’ll need to implement the UserIdSource interface as approprate for your application’s security mechanism. The UserIdSource interface looks like this:

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

The getUserId() method simply returns a String that uniquely identifies the current user.

更多信息here

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

#security.basic.enabled=false