6 date: "2016-01-22T23:10:04+00:00"
7 guid: http://juplo.de/?p=592
19 title: 'Develop a Facebook-App with Spring-Social - Part II: How Spring Social Works'
20 url: /develop-a-facebook-app-with-spring-social-part-02-how-spring-social-works/
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").
25 In [the last part of this series](/develop-a-facebook-app-with-spring-social-part-01-behind-the-scenes/ "Read part 1 of this series, to take a look behind the scenes!"), we took control of the autoconfiguration, that Spring Boot had put in place for us.
26 But there is still a lot of magic in our little example, that was borrowed from [the offical "Getting Started"-guides](http://spring.io/guides/gs/accessing-facebook/ "Read the official guide") or at least, it looks so.
28 ## First Time In The Electric-Wonder-Land
30 When I first run the example, I wondered like _"Wow, how does this little piece of code figures out which data to fetch? How is Spring Social told, which data to fetch? That must be stored in the session, or so! But where is that configured?"_ and so on and so on.
32 When we connect to Facebook, Facebook tells Spring Social, which user is logged in and if this user authorizes the requested access.
33 We get an access-token from facebook, that can be used to retrieve user-related data from the Graph-API.
34 Our application has to manage this data.
36 Spring Social assists us on that task.
37 But in the end, we have to make the decisions, how to deal with it.
39 ## Whom Are You Intrested In?
41 Spring Social provides the concept of a `ConnectionRepository`, which is used to persist the connections of specific user.
42 Spring Social also provides the concept of a `UsersConnectionRepository`, which stores, whether a user is connected to a specific social service or not.
43 As described in [the official documentation](http://docs.spring.io/spring-social/docs/1.1.4.RELEASE/reference/htmlsingle/#configuring-connectcontroller "For further details, please read the official implementations"), Spring Social uses the `UsersConnectionRepository` to create a request-scoped `ConnectionRepository` bean (the bean named `facebook` in [our little example](/develop-a-facebook-app-with-spring-social-part-00/#HomeController "Go back to part 00, to reread the code-example, that uses this bean to access the facebook-data")), that is used by us to access the Graph-API.
45 **But to be able to do so, it must know _which user_ we are interested in!**
47 Hence, Spring Social requires us to configure a `UserIdSource`.
48 Every time, when it prepares a request for us, Spring Social will ask this source, which user we are interested in.
50 Attentive readers might have noticed, that we have configured such a source, when we were [explicitly rebuilding](/develop-a-facebook-app-with-spring-social-part-01-behind-the-scenes/ "Jump back to re-read our explicitly rebuild configuration") the automatic default-configuration of Spring Boot:
53 public class AnonymousUserIdSource implements UserIdSource
56 public String getUserId()
67 All the time we are only interested in one and the same user, whose connections should be stored under the key `anonymous`?
69 **And what will happen, if a second user connects to our app?**
73 To see what happens, if more than one user connects to your app, you have to create a [test user](https://developers.facebook.com/docs/apps/test-users "Read more about test users").
75 Just go to the dashboard of your app, select the menu-item _"Roles"_ and click on the tab _"Test Users"_.
76 Select a test user (or create a new one) and click on the _"Edit"_-button.
77 There you can select _"Log in as this test user"_.
79 **If you first connect to the app as yourself and afterwards as test user, you will see, that your data is presented to the test user.**
81 That is, because we are telling Spring Social that every user is called `anonymous`.
82 Hence, every user is the same for Spring Social!
83 When the test user fetches the page, after you have connected to Facebook as yourself, Spring-Social is thinking, that the same user is returning and serves your data.
87 In [the next part](develop-a-facebook-app-with-spring-social-part-03-implementing-a-user-id-source "Jump to the next part of this series and read on...") of this series, we will try to teach Spring Social to distinguish between several users.
89 ## Funded by the Europian Union
91 This article was published in the course of a
92 [resarch-project](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project"),
93 that is funded by the European Union and the federal state Northrhine-Wetphalia.
95 [](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project")