0

My configuration is as below.

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.security.AuthenticationNameUserIdSource;

@Configuration
@EnableSocial
@PropertySource("classpath:social.properties")
public class SocialConfig extends SocialConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private Environment env;

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
        FacebookConnectionFactory facebookConnectionFactory = new FacebookConnectionFactory(
                this.env.getProperty("facebook.clientId"), 
                this.env.getProperty("facebook.clientSecret"));

        cfConfig.addConnectionFactory(facebookConnectionFactory);
    }

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        return new JdbcUsersConnectionRepository (
                dataSource,
                connectionFactoryLocator,
                Encryptors.noOpText() // refer http://stackoverflow.com/questions/12619986/what-is-the-correct-way-to-configure-a-spring-textencryptor-for-use-on-heroku
        );
        //https://github.com/naturalprogrammer/spring-boot-security-social-sample/tree/master/src
        // Create the table in the database by executing the commands at
        // http://docs.spring.io/spring-social/docs/1.1.0.RELEASE/reference/htmlsingle/#section_jdbcConnectionFactory
        // and then execute
        // create unique index UserConnectionProviderUser on UserConnection(providerId, providerUserId);

    }

    @Bean
    public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator, UsersConnectionRepository connectionRepository) {
        return new ProviderSignInUtils(connectionFactoryLocator, connectionRepository);
    };

    @Bean
    @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
    public Facebook facebook(ConnectionRepository repository) {
        Connection<Facebook> connection = repository.findPrimaryConnection(Facebook.class);
        return connection != null ? connection.getApi() : null;
    }

    @Override
    public UserIdSource getUserIdSource() {
        return new AuthenticationNameUserIdSource();
    }

}

I need to create a @bean of ConnectController, because I need to check if an account is associated with facebook account or not. And I need to disconnect or connect to facebook account. Except for these functionalities, I can use org.springframework.social.facebook.api.Facebook to pull or publish data to facebook.

So when I try to create a the ConnectController bean in this configuration, I cannot do it. As shown below, I do not have connectionFactoryLocator() and connectionRepository().

@Bean
public ConnectController connectController() {
    ConnectController controller = new ConnectController(connectionFactoryLocator(), connectionRepository());
    controller.setApplicationUrl(env.getRequiredProperty("application.url"));
    return controller;
}

I do not know how to create these two methods. Also, I wonder why this configuration works without these two methods. Everything works, but the ConnectController.

One workaround would be that I create a customized ConnectController that extends SpringSoical's ConnectController. However, I do not know if this is a proper way.

Thanks,

toby_yo
  • 179
  • 2
  • 15

1 Answers1

0

What about:

@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator,
                                           ConnectionRepository connectionRepository) {
    ConnectController controller = new ConnectController(connectionFactoryLocator, connectionRepository);
    controller.setApplicationUrl(env.getRequiredProperty("application.url"));
    return controller;
}
Tomas Marik
  • 4,053
  • 3
  • 31
  • 62