1
我想通過Spring Security(我使用註釋基礎配置)將Spring Social Facebook集成到我的應用程序中。我需要的只是連接Facebook帳戶與我的應用程序的帳戶。在簡單的例子,我發現這一點:春季社交Facebook +春季安全權限電子郵件和用戶名範圍
package eu.thinking4u.spring.social.simple.signinmvc.config;
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.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.SocialConfigurer;
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.ConnectController;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.security.AuthenticationNameUserIdSource;
@Configuration
@EnableSocial
public class SocialContext implements SocialConfigurer {
@Autowired
private DataSource dataSource;
/**
* Configures the connection factories for Facebook and Twitter.
* @param cfConfig
* @param env
*/
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
/* cfConfig.addConnectionFactory(new TwitterConnectionFactory(
env.getProperty("twitter.consumer.key"),
env.getProperty("twitter.consumer.secret")
));*/
/*cfConfig.addConnectionFactory(new FacebookConnectionFactory(
env.getProperty("facebook.app.id"),
env.getProperty("facebook.app.secret")
));*/
final FacebookConnectionFactory fbcf = new FacebookConnectionFactory(env.getProperty("facebook.app.id"),
env.getProperty("facebook.app.secret"));
fbcf.setScope("email");
cfConfig.addConnectionFactory(fbcf);
}
/**
* The UserIdSource determines the account ID of the user. The example application
* uses the username as the account ID.
*/
public UserIdSource getUserIdSource() {
return new AuthenticationNameUserIdSource();
}
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
return new JdbcUsersConnectionRepository(
dataSource,
connectionFactoryLocator,
/**
* The TextEncryptor object encrypts the authorization details of the connection. In
* our example, the authorization details are stored as plain text.
* DO NOT USE THIS IN PRODUCTION.
*/
Encryptors.noOpText()
);
}
/**
* This bean manages the connection flow between the account provider and
* the example application.
*/
@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}
/* @Bean
public CanvasSignInController canvasSignInController(ConnectionFactoryLocator connectionFactoryLocator,
UsersConnectionRepository usersConnectionRepository, Environment env)
{
return new CanvasSignInController(connectionFactoryLocator, usersConnectionRepository,
new SimpleSignInAdapter(), env.getProperty("facebook.appKey"), env.getProperty("facebook.appSecret"),
env.getProperty("facebook.canvasPage"));
}*/
}
在Facebook登錄後,接下來我們嘗試像
WebRequest request
RegistrationForm dto = new RegistrationForm();
Connection<?> connection = ProviderSignInUtils.getConnection(request);
if (connection != null) {
UserProfile socialMediaProfile = connection.fetchUserProfile();
System.out.println("DTO EMAIL--->"+socialMediaProfile.getEmail());
dto.setEmail(socialMediaProfile.getEmail());
dto.setFirstName(socialMediaProfile.getFirstName());
dto.setLastName(socialMediaProfile.getLastName());
}
我試圖讓電子郵件和用戶名來獲取用戶的個人資料,但並不working.And名字和姓氏正確提取。
你是什麼意思與*「我嘗試配置電子郵件和用戶名範圍,但不起作用。「*?目前我不知道你想做什麼。 – 2014-09-24 08:03:24
我已更新我的代碼。 – user2823228 2014-09-24 08:59:20