X-Git-Url: http://juplo.de/gitweb/?p=examples%2Ffacebook-app;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FSocialAuthenticationEntryPoint.java;h=f1193143cb21c7f0f7709fbc05f40082b76c4d98;hp=4c3671c0f7a9b9a51c0acc996b044cf9c0ebc6b1;hb=2eb6c7a9db8500a78e896e81de7045090c8e1013;hpb=8f6d3c83aa9651e593b57b3d47cfd50a4ae73661 diff --git a/src/main/java/de/juplo/yourshouter/SocialAuthenticationEntryPoint.java b/src/main/java/de/juplo/yourshouter/SocialAuthenticationEntryPoint.java index 4c3671c..f119314 100644 --- a/src/main/java/de/juplo/yourshouter/SocialAuthenticationEntryPoint.java +++ b/src/main/java/de/juplo/yourshouter/SocialAuthenticationEntryPoint.java @@ -4,6 +4,7 @@ import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.core.AuthenticationException; @@ -24,12 +25,24 @@ public class SocialAuthenticationEntryPoint implements AuthenticationEntryPoint private static final Logger LOG = LoggerFactory.getLogger(SocialAuthenticationEntryPoint.class); + public final static String REDIRECT_ATTRIBUTE = + SocialAuthenticationEntryPoint.class.getCanonicalName() + ".REDIRECT"; + /** * {@inheritDoc} * - * To commence the sign-in through the Graph-API, we only have to redirect + * To commence the sign-in through the Graph-API, we have to redirect * to our already implemented sign-in-page. + *

+ * We store the originally requested page in the {@link HttpSession}, to be + * redirect back to that page after a successful authentication in + * {@link SpringSecuritySignInAdapter}. + *

+ * Only the first request of a ressource, that requires authentication, will + * trigger the redirect to the sing-in-page. + * + * @see SpringSecuritySignInAdapter */ @Override public void commence( @@ -41,10 +54,29 @@ public class SocialAuthenticationEntryPoint implements AuthenticationEntryPoint IOException, ServletException { - LOG.info( - "redirecting unauthenticated request {} to /signin.html", - request.getRequestURI() - ); - response.sendRedirect("/signin.html"); + HttpSession session = request.getSession(); + if (session.getAttribute(REDIRECT_ATTRIBUTE) == null) + { + LOG.info( + "redirecting unauthenticated request to {}", + request.getRequestURI() + ); + StringBuffer url = request.getRequestURL(); + if (request.getQueryString() != null) + { + url.append('?'); + url.append(request.getQueryString()); + } + session.setAttribute(REDIRECT_ATTRIBUTE, url.toString()); + response.sendRedirect("/signin.html"); + } + else + { + LOG.info( + "redirect to sign-in already in progress, forbidding access to {}", + request.getRequestURI() + ); + response.sendError(HttpServletResponse.SC_FORBIDDEN); + } } }