]> juplo.de Git - website/blob
7f4983e1624e5383f7be4c67d934a7ff38ce34a4
[website] /
1 ---
2 _edit_last: "2"
3 categories:
4   - howto
5 date: "2016-01-26T14:34:23+00:00"
6 guid: http://juplo.de/?p=644
7 parent_post_id: null
8 post_id: "644"
9 tags:
10   - createmedia.nrw
11   - facebook
12   - graph-api
13   - java
14   - oauth2
15   - spring
16   - spring-boot
17   - spring-social
18 title: 'Develop a Facebook-App with Spring-Social – Part V: Refactor The Redirect-Logic'
19 url: /develop-a-facebook-app-with-spring-social-part-05-refactor-the-redirect-logic/
20
21 ---
22 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")
23
24 In [the last part of this series](/develop-a-facebook-app-with-spring-social-part-04-signing-in-users "Go back to part 4 of this series, to learn how to sign in users"), we reconfigured our app, so that users are signed in after an authentication against Facebook and new users are signed up automatically on the first visit.
25
26 In this part, we will refactor our redirect-logic for unauthenticated users, so that it more closely resembles the behavior of Spring Social, hence, easing the planed switch to that technology in a feature step.
27
28 ## The Source is With You
29
30 You can find the source-code on [/git/examples/facebook-app/](/git/examples/facebook-app/ "Link for cloning")
31 and [browse it via gitweb](/gitweb/?p=examples/facebook-app;a=summary "Browse the source-code now").
32 Check out `part-05` to get the source for this part of the series.
33
34 ## Mimic Spring Security
35
36 **To stress that again: our simple authentication-concept is only meant for educational purposes. [It is inherently insecure!](/develop-a-facebook-app-with-spring-social-part-03-implementing-a-user-id-source#remember "Jump back to part 3 to learn, why our authentication-concept is insecure")**
37 We are not refining it here, to make it better or more secure.
38 We are refining it, so that it can be replaced with Spring Security later on, without a hassle!
39
40 In our current implementation, a user, who is not yet authenticated, would be redirected to our sign-in-page only, if he visits the root of our webapp ( `/`).
41 To move all redirect-logic out of `HomeController` and redirect unauthenicated users from all pages to our sign-in-page, we can simply modify our interceptor `UserCookieInterceptor`, which already intercepts each and every request.
42
43 We refine the method `preHandle`, so that it redirects every request to our sign-in-page, that is not authenticated:
44
45 ```Java
46 @Override
47 public boolean preHandle(
48     HttpServletRequest request,
49     HttpServletResponse response,
50     Object handler
51     )
52     throws
53       Exception
54 {
55   if (request.getServletPath().startsWith("/signin"))
56     return true;
57
58   String user = UserCookieGenerator.INSTANCE.readCookieValue(request);
59   if (user != null)
60   {
61     if (!repository
62         .findUserIdsConnectedTo("facebook", Collections.singleton(user))
63         .isEmpty()
64         )
65     {
66       LOG.info("loading user {} from cookie", user);
67       SecurityContext.setCurrentUser(user);
68       return true;
69     }
70     else
71     {
72       LOG.warn("user {} is not known!", user);
73       UserCookieGenerator.INSTANCE.removeCookie(response);
74     }
75   }
76
77   response.sendRedirect("/signin.html");
78   return false;
79 }
80
81 ```
82
83 If the user, that is identified by the cookie, is not known to Spring Security, we send a redirect to our sign-in-page and flag the request as already handled, by returning `false`.
84 To prevent an endless loop of redirections, we must not redirect request, that were already redirected to our sign-in-page.
85 Since these requests hit our webapp as a new request for the different location, we can filter out and wave through at the beginning of this method.
86
87 ## Run It!
88
89 That is all there is to do.
90 Run the app and call the page `http://localhost:8080/profile.html` as first request.
91 You will see, that you will be redirected to our sigin-in-page.
92
93 ## Cleaning Up Behind Us...
94
95 As it is now not possible, to call any page except the sigin-up-page, without beeing redirected to our sign-in-page, if you are not authenticated, it is impossible to call any page without being authenticated.
96 Hence, we can (and should!) refine our `UserIdSource`, to throw an exception, if that happens anyway, because it has to be a sign for a bug:
97
98 ```Java
99 public class SecurityContextUserIdSource implements UserIdSource
100 {
101
102   @Override
103   public String getUserId()
104   {
105     Assert.state(SecurityContext.userSignedIn(), "No user signed in!");
106     return SecurityContext.getCurrentUser();
107   }
108 }
109
110 ```
111
112 ## Coming Next...
113
114 In the next part of this series, we will enable users to sign in through the canvas-page of our app.
115 The canvas-page is the page that Facebook embeds into its webpage, if we render our app inside of Facebook.