Meine Konfiguration ist wie folgt.erstellen ConnectController in SocialConfigurerAdapter
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();
}
}
Ich brauche eine @bean von ConnectController zu schaffen, weil ich überprüfen müssen, wenn ein Konto mit Facebook-Konto oder nicht zugeordnet ist. Und ich muss die Verbindung zum Facebook-Konto trennen oder herstellen. Außer diesen Funktionalitäten kann ich org.springframework.social.facebook.api.Facebook
verwenden, um Daten nach Facebook zu ziehen oder zu veröffentlichen.
Wenn ich also versuche, eine ConnectController Bean in dieser Konfiguration zu erstellen, kann ich es nicht tun. Wie unten gezeigt, habe ich keine connectionFactoryLocator()
und connectionRepository()
.
@Bean
public ConnectController connectController() {
ConnectController controller = new ConnectController(connectionFactoryLocator(), connectionRepository());
controller.setApplicationUrl(env.getRequiredProperty("application.url"));
return controller;
}
Ich weiß nicht, wie diese beiden Methoden erstellt werden. Ich frage mich auch, warum diese Konfiguration ohne diese beiden Methoden funktioniert. Alles funktioniert, aber der ConnectController.
Eine Problemumgehung wäre, dass ich einen angepassten ConnectController erstelle, der den ConnectController von SpringSoical erweitert. Ich weiß jedoch nicht, ob das ein richtiger Weg ist.
Danke,