diff --git a/web/src/main/java/org/springframework/security/web/authentication/AbstractAuthenticationTargetUrlRequestHandler.java b/web/src/main/java/org/springframework/security/web/authentication/AbstractAuthenticationTargetUrlRequestHandler.java index 73acda5aee..48a1b38996 100644 --- a/web/src/main/java/org/springframework/security/web/authentication/AbstractAuthenticationTargetUrlRequestHandler.java +++ b/web/src/main/java/org/springframework/security/web/authentication/AbstractAuthenticationTargetUrlRequestHandler.java @@ -19,25 +19,29 @@ import org.springframework.util.StringUtils; /** * Base class containing the logic used by strategies which handle redirection to a URL and - * are passed an Authentication object as part of the contract. + * are passed an {@code Authentication} object as part of the contract. * See {@link AuthenticationSuccessHandler} and * {@link org.springframework.security.web.authentication.logout.LogoutSuccessHandler LogoutSuccessHandler}, for example. *

* Uses the following logic sequence to determine how it should handle the forward/redirect *

* @@ -52,6 +56,7 @@ public abstract class AbstractAuthenticationTargetUrlRequestHandler { private String defaultTargetUrl = "/"; private boolean alwaysUseDefaultTargetUrl = false; private boolean useReferer = false; + private boolean useTargetUrlparameter = false; private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); protected AbstractAuthenticationTargetUrlRequestHandler() { @@ -83,18 +88,22 @@ public abstract class AbstractAuthenticationTargetUrlRequestHandler { } // Check for the parameter and use that if available - String targetUrl = request.getParameter(targetUrlParameter); + String targetUrl = null; - if (StringUtils.hasText(targetUrl)) { - try { - targetUrl = URLDecoder.decode(targetUrl, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException("UTF-8 not supported. Shouldn't be possible"); + if (useTargetUrlparameter) { + targetUrl = request.getParameter(targetUrlParameter); + + if (StringUtils.hasText(targetUrl)) { + try { + targetUrl = URLDecoder.decode(targetUrl, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException("UTF-8 not supported. Shouldn't be possible"); + } + + logger.debug("Found targetUrlParameter in request: " + targetUrl); + + return targetUrl; } - - logger.debug("Found targetUrlParameter in request: " + targetUrl); - - return targetUrl; } if (useReferer && !StringUtils.hasLength(targetUrl)) { @@ -112,7 +121,7 @@ public abstract class AbstractAuthenticationTargetUrlRequestHandler { /** * Supplies the default target Url that will be used if no saved request is found or the - * alwaysUseDefaultTargetUrl property is set to true. If not set, defaults to /. + * {@code alwaysUseDefaultTargetUrl} property is set to true. If not set, defaults to {@code /}. * * @return the defaultTargetUrl property */ @@ -122,7 +131,7 @@ public abstract class AbstractAuthenticationTargetUrlRequestHandler { /** * Supplies the default target Url that will be used if no saved request is found in the session, or the - * alwaysUseDefaultTargetUrl property is set to true. If not set, defaults to /. It + * {@code alwaysUseDefaultTargetUrl} property is set to true. If not set, defaults to {@code /}. It * will be treated as relative to the web-app's context path, and should include the leading /. * Alternatively, inclusion of a scheme name (such as "http://" or "https://") as the prefix will denote a * fully-qualified URL and this is also supported. @@ -136,7 +145,7 @@ public abstract class AbstractAuthenticationTargetUrlRequestHandler { } /** - * If true, will always redirect to the value of defaultTargetUrl + * If true, will always redirect to the value of {@code defaultTargetUrl} * (defaults to false). */ public void setAlwaysUseDefaultTargetUrl(boolean alwaysUseDefaultTargetUrl) { @@ -174,9 +183,19 @@ public abstract class AbstractAuthenticationTargetUrlRequestHandler { } /** - * If set to true the Referer header will be used (if available). Defaults to false. + * If set to {@code true} the {@code Referer} header will be used (if available). Defaults to {@code false}. */ public void setUseReferer(boolean useReferer) { this.useReferer = useReferer; } + + /** + * If set to {@code true} the request parameter {@code targetUrlParameter} will be used (if available). Defaults + * to {@code false}. + * + * @param useTargetUrlparameter + */ + public void setUseTargetUrlparameter(boolean useTargetUrlparameter) { + this.useTargetUrlparameter = useTargetUrlparameter; + } } diff --git a/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java b/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java index b7e1cfa572..591efdd32d 100644 --- a/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java +++ b/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java @@ -44,6 +44,7 @@ public class SimpleUrlAuthenticationSuccessHandlerTests { @Test public void targetUrlParameterIsUsedIfPresent() throws Exception { SimpleUrlAuthenticationSuccessHandler ash = new SimpleUrlAuthenticationSuccessHandler("/defaultTarget"); + ash.setUseTargetUrlparameter(true); ash.setTargetUrlParameter("targetUrl"); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse();