1 package de.juplo.yourshouter;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
7 import org.springframework.security.core.context.SecurityContext;
8 import org.springframework.security.core.context.SecurityContextHolder;
9 import org.springframework.social.connect.Connection;
10 import org.springframework.social.connect.web.SignInAdapter;
11 import org.springframework.stereotype.Service;
12 import org.springframework.web.context.request.NativeWebRequest;
16 * Simple implementation of {@link SignInAdapter}.
18 * This implementation signes in the user by storing him in the
19 * {@link SecurityContext} provided by Spring-Security, using the user-ID as
22 * We configured Spring-Social to call this implementation, to sign in the
23 * user, after he was authenticated by Facebook.
28 public class SpringSecuritySignInAdapter implements SignInAdapter
30 private final static Logger LOG =
31 LoggerFactory.getLogger(SpringSecuritySignInAdapter.class);
34 * Stores the user in the {@link SecurityContext} provided by Spring Security
35 * to sign him in. Spring Security will automatically persist the
36 * authentication in the user-session for subsequent requests.
39 * The user-ID. We configured Spring-Social to call
40 * {@link UserCookieSignInAdapter} to extract a user-ID from the
43 * The connection. In our case a connection to Facebook.
45 * The actual request. We need it, to store the cookie.
47 * We return <code>null</code>, to indicate, that the user should be
48 * redirected to the default-post-sign-in-URL (configured in
49 * {@link ProviderSinInController}) after a successfull authentication.
51 * @see {@link ProviderSignInController#postSignInUrl}
56 Connection<?> connection,
57 NativeWebRequest request
61 "signing in user {} (connected via {})",
63 connection.getKey().getProviderId()
66 SecurityContextHolder.getContext().setAuthentication(
67 new UsernamePasswordAuthenticationToken(user, null, null));
69 // We return null to trigger a redirect to "/".