Spring Social Login: How to add more than one app of the same social provider (f.e. Facebook)?

185 Views Asked by At

My Spring MVC web application is serving simultanously three different web shops with three different domains (let's call them domain1.com, domain2.com and domain3.com).

For each of these shops I need to offer an own Social Login (with custom login screen title, custom data privacy statement, ...). Therefore I need to create three different social provider (f.e. facebook) apps.

In Spring Social - from my understanding - I'm allowed to add only one FacebookConnectionFactory and hereby only one provider app.

How can I despite that add these three different facebook apps to Spring Social? Is Spring Social capable to manage this? Are there workarounds existing?

1

There are 1 best solutions below

0
user6904265 On

May be I answered too late for the OP but this could be usefull for other developers. I implemented a workaround for this issue defining my own "Registries" as follow:

<bean id="myConnectionFactoryRegistries" class="package.ShopsFactoryRegistries">
    <property name="registries">
    <map key-type="java.lang.String">
        <entry key="shop1Key" value-ref="connectionFactoryRegistryShop1" />
        <entry key="shop2Key" value-ref="connectionFactoryRegistryShop2" />
    </map>
    </property>
</bean>

<bean id="connectionFactoryRegistryShop1"
    class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
    <property name="connectionFactories">
        <list>
            <bean
                class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                <constructor-arg value="${facebook.apikey.shop1}" />
                <constructor-arg value="${facebook.apisecret.shop1}" />
            </bean>
        </list>
    </property>
</bean>

<bean id="connectionFactoryRegistryShop2"
    class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
    <property name="connectionFactories">
        <list>
            <bean
                class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                <constructor-arg value="${facebook.apikey.shop2}" />
                <constructor-arg value="${facebook.apisecret.shop2}" />
            </bean>
        </list>
    </property>
</bean>

And implement a factory in order to retrieve the proper ConnectionFactoryRegistry related to the "current" shop (identified by the shopKey).

public class ShopsFactoryRegistries
{
    private Map<String, ConnectionFactoryRegistry> registries;

    public ConnectionFactoryRegistry getRegistryForShopKey(String shopKey)
    {
        return registries.get(shopKey);
    }

    //getter and setter for registries
}

With this code you can retrieve the facebookConnectionFactory for the shop1:

String shopKey = "shop1Key";
FacebookConnectionFactory facebookConnectionFactory = (FacebookConnectionFactory) myConnectionFactoryRegistries.getRegistryForShopKey(shopKey).getConnectionFactory(FACEBOOK);