X-Git-Url: https://juplo.de/gitweb/?p=examples%2Ffacebook-app;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FUserCookieSignInAdapter.java;fp=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FUserCookieSignInAdapter.java;h=ed1a8d538ca32ca13fb1e1ab905991b44eade3c9;hp=0000000000000000000000000000000000000000;hb=a1ad44fc308e479f9a005aa2d87cb604d6eb0e7d;hpb=02f599692669d48f9865764fda994ad61d203ffb diff --git a/src/main/java/de/juplo/yourshouter/UserCookieSignInAdapter.java b/src/main/java/de/juplo/yourshouter/UserCookieSignInAdapter.java new file mode 100644 index 0000000..ed1a8d5 --- /dev/null +++ b/src/main/java/de/juplo/yourshouter/UserCookieSignInAdapter.java @@ -0,0 +1,66 @@ +package de.juplo.yourshouter; + +import javax.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.social.connect.Connection; +import org.springframework.social.connect.web.SignInAdapter; +import org.springframework.web.context.request.NativeWebRequest; + + +/** + * Simple implementation of {@link SignInAdapter}. + * + * We configured Spring-Social to call this implementation, to sign in the + * user, after he was authenticated by Facebook. + * + * @author Kai Moritz + */ +public class UserCookieSignInAdapter implements SignInAdapter +{ + private final static Logger LOG = + LoggerFactory.getLogger(UserCookieSignInAdapter.class); + + + /** + * Stores the user in the security-context to sign him in. + * Also remembers the user for subsequent calls by storing the ID in the + * cookie. + * + * @param user + * The user-ID. We configured Spring-Social to call + * {@link UserCookieSignInAdapter} to extract a user-ID from the + * connection. + * @param connection + * The connection. In our case a connection to Facebook. + * @param request + * The actual request. We need it, to store the cookie. + * @return + * We return null, to indicate, that the user should be + * redirected to the default-post-sign-in-URL (configured in + * {@link ProviderSinInController}) after a successfull authentication. + * + * @see {@link UserCookieSignInAdapter} + * @see {@link ProviderSignInController#postSignInUrl} + */ + @Override + public String signIn( + String user, + Connection connection, + NativeWebRequest request + ) + { + LOG.info( + "signing in user {} (connected via {})", + user, + connection.getKey().getProviderId() + ); + SecurityContext.setCurrentUser(user); + UserCookieGenerator + .INSTANCE + .addCookie(user, request.getNativeResponse(HttpServletResponse.class)); + + // We return null to trigger a redirect to "/". + return null; + } +}