Spring Social's ConnectController works with one or more provider-specific ConnectionFactorys to exchange authorization details with the provider and to create connections.
Spring Social GitHub provides GitHubConnectionFactory, a ConnectionFactory for creating connections with GitHub.
So that ConnectController can find GitHubConnectionFactory, it must be registered with a ConnectionFactoryRegistry.
The following class constructs a ConnectionFactoryRegistry containing a ConnectionFactory for GitHub using Spring's Java configuration style:
@Configuration public class SocialConfig { @Bean public ConnectionFactoryLocator connectionFactoryLocator() { ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry(); registry.addConnectionFactory(new GitHubConnectionFactory( environment.getProperty("github.clientId"), environment.getProperty("github.clientSecret"))); return registry; } }
Here, a GitHub connection factory is registered with ConnectionFactoryRegistry via the addConnectionFactory() method.
If we wanted to add support for connecting to other providers, we would simply register their connection factories here in the same way as GitHubConnectionFactory.
Because client IDs and secrets may be different across environments (e.g., test, production, etc) it is recommended that these values be externalized.
As shown here, Spring 3.1's Environment is used to look up the application's client ID and secret.
Optionally, you may also configure ConnectionFactoryRegistry and GitHubConnectionFactory in XML:
<bean id="connectionFactoryLocator" class="org.springframework.social.connect.support.ConnectionFactoryRegistry"> <property name="connectionFactories"> <list> <bean class="org.springframework.social.github.connect.GitHubConnectionFactory"> <constructor-arg value="${github.clientId}" /> <constructor-arg value="${github.clientSecret}" /> </bean> </list> </property> </bean>
This is functionally equivalent to the Java-based configuration of ConnectionFactoryRegistry shown before.
The only casual difference is that the connection factories are injected as a list into the connectionFactories property rather than with the addConnectionFactory() method.
As in the Java-based configuration, the application's consumer key and secret are externalized (shown here as property placeholders).
Refer to Spring Social's reference documentation for complete details on configuring ConnectController and its dependencies.