2abcd4283f3db0844c2bd5755357c53ba318066c
[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.facebook.api.Facebook;
21 import org.springframework.social.facebook.connect.FacebookConnectionFactory;
22
23
24 /**
25  * Spring Social Configuration.
26  *
27  * @author Kai Moritz
28  */
29 @Configuration
30 @EnableSocial
31 public class SocialConfig extends SocialConfigurerAdapter
32 {
33   /**
34    * Add a {@link FacebookConnectionFactory} to the configuration.
35    * The factory is configured through the keys <code>facebook.app.id</code>
36    * and <code>facebook.app.secret</code>.
37    *
38    * @param config
39    * @param env 
40    */
41   @Override
42   public void addConnectionFactories(
43       ConnectionFactoryConfigurer config,
44       Environment env
45       )
46   {
47     config.addConnectionFactory(
48         new FacebookConnectionFactory(
49             env.getProperty("facebook.app.id"),
50             env.getProperty("facebook.app.secret")
51             )
52         );
53   }
54
55   /**
56    * Configure an instance of {@link InMemoryUsersConnection} as persistent
57    * store of user/connection-mappings.
58    *
59    * At the moment, no special configuration is needed.
60    *
61    * @param connectionFactoryLocator
62    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
63    * @return
64    *     The configured {@link UsersConnectionRepository}.
65    */
66   @Override
67   public UsersConnectionRepository getUsersConnectionRepository(
68       ConnectionFactoryLocator connectionFactoryLocator
69       )
70   {
71     InMemoryUsersConnectionRepository repository =
72         new InMemoryUsersConnectionRepository(connectionFactoryLocator);
73     repository.setConnectionSignUp(new ProviderUserIdConnectionSignUp());
74     return repository;
75   }
76
77   /**
78    * Configure our new implementation of {@link UserIdSource}, that retrieves
79    * the current user from the {@link SecurityContext}.
80    *
81    * @return
82    *     An instance of {@link AnonymousUserIdSource}.
83    *
84    * @see {@link SecurityContextUserIdSource}
85    * @see {@link SecurityContext}
86    * @see {@link UserCookieInterceptor}
87    */
88   @Override
89   public UserIdSource getUserIdSource()
90   {
91     return new SecurityContextUserIdSource();
92   }
93
94
95   /**
96    * Configuration of the controller, that handles the authorization against
97    * the Facebook-API, to connect a user to Facebook.
98    *
99    * At the moment, no special configuration is needed.
100    *
101    * @param factoryLocator
102    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
103    * @param repository
104    *     The {@link ConnectionRepository} will be injected by Spring.
105    * @return
106    *     The configured controller.
107    */
108   @Bean
109   public ConnectController connectController(
110       ConnectionFactoryLocator factoryLocator,
111       ConnectionRepository repository
112       )
113   {
114     ConnectController controller =
115         new ConnectController(factoryLocator, repository);
116     return controller;
117   }
118
119   /**
120    * Configure a scoped bean named <code>facebook</code>, that enables
121    * access to the Graph-API in the name of the current user.
122    *
123    * @param repository
124    *     The {@link ConnectionRepository} will be injected by Spring.
125    * @return
126    *     A {@Connection<Facebook>}, that represents the authorization of the
127    *     current user against the Graph-API, or <code>null</code>, if the
128    *     current user is not connected to the API.
129    */
130   @Bean
131   @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
132   public Facebook facebook(ConnectionRepository repository)
133   {
134     Connection<Facebook> connection =
135         repository.findPrimaryConnection(Facebook.class);
136     return connection != null ? connection.getApi() : null;
137   }
138 }