|
7 | 7 | import javax.servlet.http.HttpServletResponse; |
8 | 8 | import javax.servlet.http.HttpSession; |
9 | 9 |
|
| 10 | +import org.apache.commons.logging.Log; |
| 11 | +import org.apache.commons.logging.LogFactory; |
10 | 12 | import org.springframework.security.core.Authentication; |
| 13 | +import org.springframework.security.web.DefaultRedirectStrategy; |
| 14 | +import org.springframework.security.web.RedirectStrategy; |
11 | 15 | import org.springframework.security.web.WebAttributes; |
12 | | -import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler; |
13 | 16 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
| 17 | +import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper; |
14 | 18 |
|
15 | | -/** |
16 | | - * <tt>AuthenticationSuccessHandler</tt> which can be configured with a default URL which users should be |
17 | | - * sent to upon successful authentication. |
18 | | - * <p> |
19 | | - * The logic used is that of the {@link AbstractAuthenticationTargetUrlRequestHandler parent class}. |
20 | | - * |
21 | | - * @author Luke Taylor |
22 | | - * @since 3.0 |
23 | | - */ |
24 | | -public class MySimpleUrlAuthenticationSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler implements AuthenticationSuccessHandler { |
25 | | - |
26 | | - public MySimpleUrlAuthenticationSuccessHandler() { |
| 19 | +public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { |
| 20 | + protected final Log logger = LogFactory.getLog(this.getClass()); |
| 21 | + |
| 22 | + private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); |
| 23 | + |
| 24 | + protected MySimpleUrlAuthenticationSuccessHandler() { |
27 | 25 | super(); |
28 | 26 | } |
29 | 27 |
|
30 | 28 | /** |
31 | | - * Constructor which sets the <tt>defaultTargetUrl</tt> property of the base class. |
32 | | - * @param defaultTargetUrl the URL to which the user should be redirected on successful authentication. |
| 29 | + * Invokes the configured {@code RedirectStrategy} with the URL returned by the {@code determineTargetUrl} method. |
| 30 | + * <p> |
| 31 | + * The redirect will not be performed if the response has already been committed. |
| 32 | + */ |
| 33 | + protected void handle(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException { |
| 34 | + final String targetUrl = determineTargetUrl(request, response); |
| 35 | + |
| 36 | + if (response.isCommitted()) { |
| 37 | + logger.debug("Response has already been committed. Unable to redirect to " + targetUrl); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + redirectStrategy.sendRedirect(request, response, targetUrl); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Builds the target URL according to the logic defined in the main class Javadoc. |
33 | 46 | */ |
34 | | - public MySimpleUrlAuthenticationSuccessHandler(final String defaultTargetUrl) { |
35 | | - setDefaultTargetUrl(defaultTargetUrl); |
| 47 | + protected String determineTargetUrl(final HttpServletRequest requestRaw, final HttpServletResponse response) { |
| 48 | + // Check for the parameter and use that if available |
| 49 | + |
| 50 | + final SecurityContextHolderAwareRequestWrapper req = (SecurityContextHolderAwareRequestWrapper) requestRaw; |
| 51 | + final boolean isUser = req.isUserInRole("ROLE_USER"); |
| 52 | + final boolean isAdmin = req.isUserInRole("ROLE_ADMIN"); |
| 53 | + if (isUser) { |
| 54 | + return "/homepage.html"; |
| 55 | + } else if (isAdmin) { |
| 56 | + return "/console.html"; |
| 57 | + } else { |
| 58 | + throw new IllegalStateException(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Allows overriding of the behavior when redirecting to a target URL. |
| 64 | + */ |
| 65 | + public void setRedirectStrategy(final RedirectStrategy redirectStrategy) { |
| 66 | + this.redirectStrategy = redirectStrategy; |
| 67 | + } |
| 68 | + |
| 69 | + protected RedirectStrategy getRedirectStrategy() { |
| 70 | + return redirectStrategy; |
36 | 71 | } |
37 | 72 |
|
38 | 73 | /** |
|
0 commit comments