88cf1568d10bec6b26645bfff453dea0f4f31784
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / UserCookieSignInAdapter.java
1 package de.juplo.yourshouter;
2
3 import javax.servlet.http.HttpServletResponse;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6 import org.springframework.social.connect.Connection;
7 import org.springframework.social.connect.web.SignInAdapter;
8 import org.springframework.stereotype.Service;
9 import org.springframework.web.context.request.NativeWebRequest;
10
11
12 /**
13  * Simple implementation of {@link SignInAdapter}.
14  *
15  * We configured Spring-Social to call this implementation, to sign in the
16  * user, after he was authenticated by Facebook.
17  *
18  * @author Kai Moritz
19  */
20 @Service
21 public class UserCookieSignInAdapter implements SignInAdapter
22 {
23   private final static Logger LOG =
24       LoggerFactory.getLogger(UserCookieSignInAdapter.class);
25
26
27   /**
28    * Stores the user in the security-context to sign him in.
29    * Also remembers the user for subsequent calls by storing the ID in the
30    * cookie.
31    *
32    * @param user
33    *     The user-ID. We configured Spring-Social to call
34    *     {@link UserCookieSignInAdapter} to extract a user-ID from the
35    *     connection.
36    * @param connection
37    *     The connection. In our case a connection to Facebook.
38    * @param request
39    *     The actual request. We need it, to store the cookie.
40    * @return
41    *     We return <code>null</code>, to indicate, that the user should be
42    *     redirected to the default-post-sign-in-URL (configured in
43    *     {@link ProviderSinInController}) after a successfull authentication.
44    *
45    * @see {@link UserCookieSignInAdapter}
46    * @see {@link ProviderSignInController#postSignInUrl}
47    */
48   @Override
49   public String signIn(
50       String user,
51       Connection<?> connection,
52       NativeWebRequest request
53       )
54   {
55     LOG.info(
56         "signing in user {} (connected via {})",
57         user,
58         connection.getKey().getProviderId()
59         );
60     SecurityContext.setCurrentUser(user);
61     UserCookieGenerator
62         .INSTANCE
63         .addCookie(user, request.getNativeResponse(HttpServletResponse.class));
64
65     // We return null to trigger a redirect to "/".
66     return null;
67   }
68 }