ff6915147d8cab0cd187e7b34d651298c740e4ba
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SocialConfig.java
1 package de.juplo.yourshouter;
2
3
4
5 import org.springframework.context.annotation.Bean;
6 import org.springframework.context.annotation.Configuration;
7 import org.springframework.context.annotation.Scope;
8 import org.springframework.context.annotation.ScopedProxyMode;
9 import org.springframework.social.UserIdSource;
10 import org.springframework.core.env.Environment;
11 import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
12 import org.springframework.social.config.annotation.EnableSocial;
13 import org.springframework.social.config.annotation.SocialConfigurerAdapter;
14 import org.springframework.social.connect.Connection;
15 import org.springframework.social.connect.ConnectionFactoryLocator;
16 import org.springframework.social.connect.ConnectionRepository;
17 import org.springframework.social.connect.UsersConnectionRepository;
18 import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
19 import org.springframework.social.connect.web.ConnectController;
20 import org.springframework.social.connect.web.ProviderSignInController;
21 import org.springframework.social.connect.web.SignInAdapter;
22 import org.springframework.social.facebook.api.Facebook;
23 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
24
25
26 /**
27  * Spring Social Configuration.
28  *
29  * @author Kai Moritz
30  */
31 @Configuration
32 @EnableSocial
33 public class SocialConfig extends SocialConfigurerAdapter
34 {
35   /**
36    * Add a {@link FacebookConnectionFactory} to the configuration.
37    * The factory is configured through the keys <code>facebook.app.id</code>
38    * and <code>facebook.app.secret</code>.
39    *
40    * @param config
41    * @param env 
42    */
43   @Override
44   public void addConnectionFactories(
45       ConnectionFactoryConfigurer config,
46       Environment env
47       )
48   {
49     config.addConnectionFactory(
50         new FacebookConnectionFactory(
51             env.getProperty("facebook.app.id"),
52             env.getProperty("facebook.app.secret")
53             )
54         );
55   }
56
57   /**
58    * Configure an instance of {@link InMemoryUsersConnection} as persistent
59    * store of user/connection-mappings.
60    *
61    * At the moment, no special configuration is needed.
62    *
63    * @param connectionFactoryLocator
64    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
65    * @return
66    *     The configured {@link UsersConnectionRepository}.
67    */
68   @Override
69   public UsersConnectionRepository getUsersConnectionRepository(
70       ConnectionFactoryLocator connectionFactoryLocator
71       )
72   {
73     InMemoryUsersConnectionRepository repository =
74         new InMemoryUsersConnectionRepository(connectionFactoryLocator);
75     repository.setConnectionSignUp(new ProviderUserIdConnectionSignUp());
76     return repository;
77   }
78
79   /**
80    * Configure our new implementation of {@link UserIdSource}, that retrieves
81    * the current user from the {@link SecurityContext}.
82    *
83    * @return
84    *     An instance of {@link AnonymousUserIdSource}.
85    *
86    * @see {@link SecurityContextUserIdSource}
87    * @see {@link SecurityContext}
88    * @see {@link UserCookieInterceptor}
89    */
90   @Override
91   public UserIdSource getUserIdSource()
92   {
93     return new SecurityContextUserIdSource();
94   }
95
96
97   /**
98    * Configuration of the controller, that handles the authorization against
99    * the Facebook-API, to connect a user to Facebook.
100    *
101    * At the moment, no special configuration is needed.
102    *
103    * @param factoryLocator
104    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
105    * @param repository
106    *     The {@link ConnectionRepository} will be injected by Spring.
107    * @return
108    *     The configured controller.
109    */
110   @Bean
111   public ConnectController connectController(
112       ConnectionFactoryLocator factoryLocator,
113       ConnectionRepository repository
114       )
115   {
116     ConnectController controller =
117         new ConnectController(factoryLocator, repository);
118     return controller;
119   }
120
121   /**
122    * Configure the {@link ProviderSignInController} to use our implementation
123    * of {@link SignInAdapter} to sign in the user by storing the ID in the
124    * {@link SecurityContext} and the user-cookie.
125    *
126    * @param factoryLocator The {@link ConnectionFactoryLocator} will be injected by Spring.
127    * @param repository The {@link UserConnectionRepository} will be injected by Spring.
128    * @return The configured {@link ProviderSignInController}
129    */
130   @Bean
131   public ProviderSignInController signInController(
132       ConnectionFactoryLocator factoryLocator,
133       UsersConnectionRepository repository
134       )
135   {
136     ProviderSignInController controller = new ProviderSignInController(
137         factoryLocator,
138         repository,
139         new UserCookieSignInAdapter()
140         );
141     return controller;
142   }
143
144   /**
145    * Configure a scoped bean named <code>facebook</code>, that enables
146    * access to the Graph-API in the name of the current user.
147    *
148    * @param repository
149    *     The {@link ConnectionRepository} will be injected by Spring.
150    * @return
151    *     A {@Connection<Facebook>}, that represents the authorization of the
152    *     current user against the Graph-API, or <code>null</code>, if the
153    *     current user is not connected to the API.
154    */
155   @Bean
156   @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
157   public Facebook facebook(ConnectionRepository repository)
158   {
159     Connection<Facebook> connection =
160         repository.findPrimaryConnection(Facebook.class);
161     return connection != null ? connection.getApi() : null;
162   }
163 }