在SocialConfigurerAdapter中创建ConnectController

时间:2016-05-07 04:39:39

标签: java spring facebook spring-social

我的配置如下。

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();
    }

}

我需要创建一个@ Bean的ConnectController,因为我需要检查一个帐户是否与Facebook帐户相关联。我需要断开连接或连接到Facebook帐户。除了这些功能之外,我可以使用org.springframework.social.facebook.api.Facebook将数据提取或发布到Facebook。

因此,当我尝试在此配置中创建ConnectController bean时,我无法做到。如下所示,我没有connectionFactoryLocator()connectionRepository()

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

我不知道如何创建这两种方法。另外,我想知道为什么没有这两种方法这种配置。一切正常,但ConnectController。

一个解决方法是创建一个扩展SpringSoical的ConnectController的自定义ConnectController。但是,我不知道这是否正确。

谢谢,

1 个答案:

答案 0 :(得分:0)

怎么样:

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