7

I'm trying to use Facebook sign in as described in https://github.com/spring-guides/gs-accessing-facebook

When I'm trying to create JdbcUsersConnectionRepository, I need spring security in class path. And when I add spring security I receive

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

when trying to receive Connection

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

or checking

if (!facebook.isAuthorized())

All this happens only when spring security is in the class path

Pavel Bernshtam
  • 4,232
  • 8
  • 38
  • 62

2 Answers2

1

Social Connection should be correspond to any auth user. It seems user should login through username/password or Service Provider. Try look at 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"

It happens because AuthenticationNameUserIdSource used by default

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.

More info here

Community
  • 1
  • 1
Vitalii Velikodnyi
  • 1,312
  • 11
  • 18
  • Can you point me to an example of UserIdSource implementation for Facebook? In order to return a user id I need to know whether a use is connected. And I can't do it because I can't call connectionRepository.findPrimaryConnection(Facebook.class) – Pavel Bernshtam Jan 03 '16 at 20:15
  • @PavelBernshtam Hello! Why your user not login in and you try to get social connection for him (SecurityContextHolder.getContext().getAuthentication() is null)? Social Connection should be correspond to any user. It seems user should login through username/password or Service Provider. Do you try http://docs.spring.io/spring-social/docs/1.0.x/reference/html/signin.html? – Vitalii Velikodnyi Jan 03 '16 at 22:49
-1

If you are using Spring boot, then the property security basic should not be set to false. Hide this line in your application.properties. This disables the security and boot throws the error if spring-security is disabled.

#security.basic.enabled=false
Lucky
  • 16,787
  • 19
  • 117
  • 151