Developer MJ

[Spring Social] Social Configuration 본문

Framework/Spring

[Spring Social] Social Configuration

MIN JOON 2017. 8. 31. 09:46

 

- Maven pom.xml로 Spring Social Dependency 추가

 

- Application.properties 파일에 Connection 유지를 위한 H2 Database 설정과 소셜미디어 AppID와 Secret 설정

 

 

- Spring Social 관련 Configuration Bean 등록

1. ConnectionFactoryLocator

2. UsersConnectRepository

3. ConnectionRepository

4. Facebook

5. Twitter

6. Google

 

 

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.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.UserIdSource;
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.support.ConnectionFactoryRegistry;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.google.api.Google;
import org.springframework.social.google.connect.GoogleConnectionFactory;
import org.springframework.social.security.AuthenticationNameUserIdSource;
import org.springframework.social.twitter.api.Twitter;

@Configuration
public class SocialConfig extends SocialConfigurerAdapter {

	@Autowired
	private DataSource dataSource;
	@Autowired
	private Environment environment;

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

	/*
	 * 각 소셜 미디어 AppID와 Secret 등록
	 * Facebook과 Twitter는 properties에서 바로 적용가능
	*/
	@Bean
	@Scope(value = "singleton", proxyMode = ScopedProxyMode.INTERFACES)
	public ConnectionFactoryLocator connectionFactoryLocator() {
		ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
		registry.addConnectionFactory(new GoogleConnectionFactory(environment.getProperty("google.consumerKey"),
				environment.getProperty("google.consumerSecret")));
		return registry;
	}

	/*
	 * 소셜미디어와의 Connection을 database에서 관리할 수 있도록 JDBC연결
	 */
	@Bean
	@Scope(value = "singleton", proxyMode = ScopedProxyMode.INTERFACES)
	public UsersConnectionRepository usersConnectionRepository() {
		return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator(), Encryptors.noOpText());
	}
	@Bean
	@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
	public ConnectionRepository connectionRepository() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null) {
			throw new IllegalStateException("Unable to get a ConnectionRepository : no user signed in");
		}
		return usersConnectionRepository().createConnectionRepository(authentication.getName());
	}

	/*
	 * 소셜미디어의 API Binding
	 */
	@Bean
	@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
	public Facebook facebook() {
		Connection facebook = connectionRepository().findPrimaryConnection(Facebook.class);
		return facebook.getApi();
	}
	@Bean
	@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
	public Twitter twitter() {
		Connection twitter = connectionRepository().findPrimaryConnection(Twitter.class);
		return twitter.getApi();
	}
	@Bean
	@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
	public Google google(){
		Connection google = connectionRepository().findPrimaryConnection(Google.class);
		return google.getApi();
	}
}