package de.juplo.yourshouter;
import javax.inject.Inject;
import javax.sql.DataSource;
import org.apache.http.HttpRequestFactory;
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.social.UserIdSource;
import org.springframework.core.env.Environment;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.crypto.encrypt.Encryptors;
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.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.web.ProviderSignInController;
import org.springframework.social.connect.web.SignInAdapter;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.facebook.web.CanvasSignInController;
/**
* Spring Social Configuration.
*
* @author Kai Moritz
*/
@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter
{
@Inject
DataSource dataSource;
@Inject
ConnectionSignUp connectionSignUp;
@Inject
SignInAdapter signInAdapter;
/**
* Add a {@link FacebookConnectionFactory} to the configuration.
* The factory is configured through the keys facebook.app.id
* and facebook.app.secret
.
*
* @param config
* @param env
*/
@Override
public void addConnectionFactories(
ConnectionFactoryConfigurer config,
Environment env
)
{
config.addConnectionFactory(
new FacebookConnectionFactory(
env.getProperty("facebook.app.id"),
env.getProperty("facebook.app.secret")
)
);
}
/**
* {@inheritDoc}
*
* Configure an instance of {@link JdbcUsersConnection} as persistent
* store of user/connection-mappings.
*
* The app-secret is reused as password for the encryption of the data.
* The salt can be changed in the pom.xml
*
* This does only work, if you have the Java Crypto Extension (JCE) in
* full strength version, since Spring Security is using a 256-bit key.
*
* @see http://stackoverflow.com/a/17637354
*/
@Override
public UsersConnectionRepository getUsersConnectionRepository(
ConnectionFactoryLocator connectionFactoryLocator
)
{
JdbcUsersConnectionRepository repository =
new JdbcUsersConnectionRepository(
dataSource,
connectionFactoryLocator,
Encryptors.noOpText()
);
repository.setConnectionSignUp(connectionSignUp);
return repository;
}
/**
* Configure our new implementation of {@link UserIdSource}, that retrieves
* the current user from the {@link SecurityContext}.
*
* @return
* An instance of {@link AnonymousUserIdSource}.
*
* @see {@link SecurityContextUserIdSource}
* @see {@link SecurityContext}
* @see {@link UserCookieInterceptor}
*/
@Override
public UserIdSource getUserIdSource()
{
return new SpringSecurityContextUserIdSource();
}
/**
* Configuration of the controller, that handles the authorization against
* the Facebook-API, to connect a user to Facebook.
*
* At the moment, no special configuration is needed.
*
* @param factoryLocator
* The {@link ConnectionFactoryLocator} will be injected by Spring.
* @param repository
* The {@link ConnectionRepository} will be injected by Spring.
* @return
* The configured controller.
*/
@Bean
public ConnectController connectController(
ConnectionFactoryLocator factoryLocator,
ConnectionRepository repository
)
{
ConnectController controller =
new ConnectController(factoryLocator, repository);
return controller;
}
/**
* Configure the {@link ProviderSignInController} to use our implementation
* of {@link SignInAdapter} to sign in the user by storing the ID in the
* {@link SecurityContext} and the user-cookie.
*
* @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
* @param repository The {@link UserConnectionRepository} will be injected by Spring.
* @return The configured {@link ProviderSignInController}
*/
@Bean
public ProviderSignInController signInController(
ConnectionFactoryLocator factoryLocator,
UsersConnectionRepository repository
)
{
ProviderSignInController controller =
new ProviderSignInController(factoryLocator, repository, signInAdapter);
return controller;
}
/**
* Configure the {@link CanvasSignInController} to enable sign-in through
* the signed_request
, that Facebook sends to the canvas-page.
*
* @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
* @param repository The {@link UserConnectionRepository} will be injected by Spring.
* @param env The {@link Environment}, to read additional parameters from.
* @return The configured {@link CanvasSignInController}
*/
@Bean
public CanvasSignInController canvasSignInController(
ConnectionFactoryLocator factoryLocator,
UsersConnectionRepository repository,
Environment env
)
{
return
new CanvasSignInController(
factoryLocator,
repository,
signInAdapter,
env.getProperty("facebook.app.id"),
env.getProperty("facebook.app.secret"),
env.getProperty("facebook.app.canvas")
);
}
/**
* Configure a scoped bean named facebook
, that enables
* access to the Graph-API in the name of the current user.
*
* @param repository
* The {@link ConnectionRepository} will be injected by Spring.
* @return
* A {@Connectionnull
, if the
* current user is not connected to the API.
*/
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Facebook facebook(ConnectionRepository repository)
{
ConnectionHttpClient
from Apaches HttpComponents
* for HTTP-requests.
*
* We also configure shorter intervals for the connection timeout and the
* read timeout.
*
* @param env The {@link Environment}, to read additional parameters from.
* @return The alternative implementation of {@link HttpRequestFactory}.
*/
@Bean
public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
{
HttpComponentsClientHttpRequestFactory factory =
new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(
Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
);
factory.setReadTimeout(
Integer.parseInt(env.getProperty("httpclient.timeout.read"))
);
return factory;
}
}