]> juplo.de Git - website/blob
d0969e26f86a275464896e07ae44d8fa709c4c73
[website] /
1 ---
2 _edit_last: "2"
3 author: kai
4 categories:
5   - howto
6 date: "2016-01-22T16:19:12+00:00"
7 guid: http://juplo.de/?p=579
8 parent_post_id: null
9 post_id: "579"
10 tags:
11   - createmedia.nrw
12   - facebook
13   - graph-api
14   - java
15   - oauth2
16   - spring
17   - spring-boot
18   - spring-social
19 title: 'Develop a Facebook-App with Spring-Social - Part I: Behind the Scenes'
20 url: /develop-a-facebook-app-with-spring-social-part-01-behind-the-scenes/
21
22 ---
23 In this series of Mini-How-Tow's I will describe how to develop a facebook app with the help of [Spring-Social](http://projects.spring.io/spring-social/ "Learn more about Spring-Social").
24
25 In [the last and first part of this series](/develop-a-facebook-app-with-spring-social-part-00/ "Read part 0 of this series, to get prepared!"), I prepared you for our little course.
26
27 In this part we will take a look behind the scenes and learn more about the autoconfiguration performed by Spring-Boot, which made our first small example so automagically.
28
29 ## The Source is With You
30
31 You can find the source-code on [/git/examples/facebook-app/](/git/examples/facebook-app/ "Link for cloning")
32 and [browse it via gitweb](/gitweb/?p=examples/facebook-app;a=summary "Browse the source-code now").
33 Check out `part-01` to get the source for this part of the series.
34
35 ## Our Silent Servant Behind the Scenes: Spring-Boot
36
37 While looking at our simple example from the last part of this series, you may have wondered, how all this is wired up.
38 You can log in a user from facebook, access his public profile and all this without one line of configuration.
39
40 **This is achieved via [Spring-Boot autoconfiguration](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-auto-configuration "Learn more about Spring-Boot's autoconfiguration-mechanism").**
41
42 What comes in very handy in the beginning, sometimes get's in your way, when your project grows.
43 This may happen, because these parts of the code are not under your control and you do not know what the autoconfiguration is doing on your behalf.
44 Because of that, in this part of our series, we will rebuild the most relevant parts of the configuration by hand.
45 As you will see later, this is not only an exercise, but will lead us to the first improvement of our little example.
46
47 ## What Is Going On Here?
48
49 In our case, two Spring-Boot configuration-classes are defining the configuration.
50 These two classes are [SocialWebAutoConfiguration](https://github.com/spring-projects/spring-boot/blob/v1.3.1.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java "View the class on github") and [FacebookAutoConfiguration](https://github.com/spring-projects/spring-boot/blob/v1.3.1.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java "View the class on github").
51 Both classes are located in the package [spring-boot-autoconfigure](https://github.com/spring-projects/spring-boot/tree/v1.3.1.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social "View the package on github").
52
53 The first one configures the `ConnectController`, sets up an instance of `InMemoryUsersConnectionRepository` as persitent store for user/connection-mappings and sets up a `UserIdService` on our behalf, that always returns the user-id `anonymous`.
54
55 The second one adds an instance of `FacebookConnectionFactory` to the list of available connection-factories, if the required properties ( `spring.social.facebook.appId` and `spring.social.facebook.appSecret`) are available.
56 It also configures, that a request-scoped bean of the type `Connection<Facebook>` is created for each request, that has a known user, who is connected to the Graph-API.
57
58 ## Rebuild This Configuration By Hand
59
60 The following class rebuilds the same configuration explicitly:
61
62 ```Java
63 @Configuration
64 @EnableSocial
65 public class SocialConfig extends SocialConfigurerAdapter
66 {
67   /**
68    * Add a {@link FacebookConnectionFactory} to the configuration.
69    * The factory is configured through the keys <code>facebook.app.id</code>
70    * and <,code>facebook.app.secret</code>.
71    *
72    * @param config
73    * @param env
74    */
75   @Override
76   public void addConnectionFactories(
77       ConnectionFactoryConfigurer config,
78       Environment env
79       )
80   {
81     config.addConnectionFactory(
82         new FacebookConnectionFactory(
83             env.getProperty("facebook.app.id"),
84             env.getProperty("facebook.app.secret")
85             )
86         );
87   }
88
89   /**
90    * Configure an instance of {@link InMemoryUsersConnection} as persistent
91    * store of user/connection-mappings.
92    *
93    * At the moment, no special configuration is needed.
94    *
95    * @param connectionFactoryLocator
96    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
97    * @return
98    *     The configured {@link UsersConnectionRepository}.
99    */
100   @Override
101   public UsersConnectionRepository getUsersConnectionRepository(
102       ConnectionFactoryLocator connectionFactoryLocator
103       )
104   {
105     InMemoryUsersConnectionRepository repository =
106         new InMemoryUsersConnectionRepository(connectionFactoryLocator);
107     return repository;
108   }
109
110   /**
111    * Configure a {@link UserIdSource}, that is equivalent to the one, that is
112    * created by Spring-Boot.
113    *
114    * @return
115    *     An instance of {@link AnonymousUserIdSource}.
116    *
117    * @see {@link AnonymousUserIdSource}
118    */
119   @Override
120   public UserIdSource getUserIdSource()
121   {
122     return new AnonymousUserIdSource();
123   }
124
125   /**
126    * Configuration of the controller, that handles the authorization against
127    * the Facebook-API, to connect a user to Facebook.
128    *
129    * At the moment, no special configuration is needed.
130    *
131    * @param factoryLocator
132    *     The {@link ConnectionFactoryLocator} will be injected by Spring.
133    * @param repository
134    *     The {@link ConnectionRepository} will be injected by Spring.
135    * @return
136    *     The configured controller.
137    */
138   @Bean
139   public ConnectController connectController(
140       ConnectionFactoryLocator factoryLocator,
141       ConnectionRepository repository
142       )
143   {
144     ConnectController controller =
145         new ConnectController(factoryLocator, repository);
146     return controller;
147   }
148
149   /**
150    * Configure a scoped bean named <code>facebook</code>, that enables
151    * access to the Graph-API in the name of the current user.
152    *
153    * @param repository
154    *     The {@link ConnectionRepository} will be injected by Spring.
155    * @return
156    *     A {@Connection}, that represents the authorization of the
157    *     current user against the Graph-API, or null, if the
158    *     current user is not connected to the API.
159    */
160   @Bean
161   @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
162   public Facebook facebook(ConnectionRepository repository)
163   {
164     Connection connection =
165         repository.findPrimaryConnection(Facebook.class);
166     return connection != null ? connection.getApi() : null;
167   }
168 }
169
170 ```
171
172 If you run this refined version of our app, you will see, that it behaves in exact the same way, as the initial version.
173
174 ## Coming next
175
176 You may ask, why we should rebuild the configuration by hand, if it does the same thing.
177 This is, because the example, so far, would not work as a real app.
178 The first step, to refine it, is to take control of the configuration.
179
180 In [the next part](develop-a-facebook-app-with-spring-social-part-02-how-spring-social-works "Jump to the third part of this series and read on...") of this series, I will show you, why this is necessary.
181 But, first, we have to take a short look into Spring Social.
182
183 ## Funded by the Europian Union
184
185 This article was published in the course of a
186 [resarch-project](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project"),
187 that is funded by the European Union and the federal state Northrhine-Wetphalia.
188
189 [![Europäische Union: Investitionen in unsere Zukunft - Europäischer Fonds für regionale Entwicklung](/img/EFRE_Foerderhinweis_deutsch_farbig.svg)![EFRE.NRW 2014-2020: Invesitionen in Wachstum und Beschäftigung](/img/Ziel2NRW_4c_1809_eps.svg)](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project")