Redirect to originally requested page after sign-in
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SpringSecuritySignInAdapter.java
1 package de.juplo.yourshouter;
2
3
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpSession;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
9 import org.springframework.security.core.context.SecurityContext;
10 import org.springframework.security.core.context.SecurityContextHolder;
11 import org.springframework.social.connect.Connection;
12 import org.springframework.social.connect.web.SignInAdapter;
13 import org.springframework.stereotype.Service;
14 import org.springframework.web.context.request.NativeWebRequest;
15
16
17 /**
18  * Simple implementation of {@link SignInAdapter}.
19  *
20  * This implementation signes in the user by storing him in the
21  * {@link SecurityContext} provided by Spring-Security, using the user-ID as
22  * principal.
23  *
24  * We configured Spring-Social to call this implementation, to sign in the
25  * user, after he was authenticated by Facebook.
26  *
27  * @author Kai Moritz
28  */
29 @Service
30 public class SpringSecuritySignInAdapter implements SignInAdapter
31 {
32   private final static Logger LOG =
33       LoggerFactory.getLogger(SpringSecuritySignInAdapter.class);
34
35   /**
36    * {@inheritDoc}
37    *
38    * Stores the user in the {@link SecurityContext} provided by Spring Security
39    * to sign him in. Spring Security will automatically persist the
40    * authentication in the user-session for subsequent requests.
41    * <p>
42    * If an originally requested ressource was stored in the {@link HttpSession}
43    * by the {@link SocialAuthenticationEntryPoint}, that URL will be returned,
44    * so that the {@link RequestCache} can restore the request.
45    * Otherwise, <code>null</code> will be returned, to indicate, that the user
46    * should be redirected to the default-post-sign-in-URL (configured in
47    * {@link ProviderSinInController}) after a successfull authentication.
48    *
49    * @see {@link SocialAuthenticationEntryPoint}
50    * @see {@link ProviderSignInController#postSignInUrl}
51    */
52   @Override
53   public String signIn(
54       String user,
55       Connection<?> connection,
56       NativeWebRequest request
57       )
58   {
59     LOG.info(
60         "signing in user {} (connected via {})",
61         user,
62         connection.getKey().getProviderId()
63         );
64
65     SecurityContextHolder.getContext().setAuthentication(
66         new UsernamePasswordAuthenticationToken(user, null, null));
67
68     HttpSession session =
69         request.getNativeRequest(HttpServletRequest.class).getSession();
70     String redirect =
71         (String)session
72             .getAttribute(SocialAuthenticationEntryPoint.REDIRECT_ATTRIBUTE);
73     if (redirect != null)
74     {
75       LOG.info("redirecting to originally requested resource {}", redirect);
76       session.removeAttribute(SocialAuthenticationEntryPoint.REDIRECT_ATTRIBUTE);
77     }
78     else
79     {
80       LOG.info("found no original request in session, redirecting to default");
81     }
82     return redirect;
83   }
84 }