|
16 | 16 | package org.springframework.security.web;
|
17 | 17 |
|
18 | 18 | import java.io.IOException;
|
19 |
| - |
20 | 19 | import javax.servlet.http.HttpServletRequest;
|
21 | 20 | import javax.servlet.http.HttpServletResponse;
|
22 | 21 |
|
23 | 22 | import org.apache.commons.logging.Log;
|
24 | 23 | import org.apache.commons.logging.LogFactory;
|
| 24 | + |
25 | 25 | import org.springframework.security.web.util.UrlUtils;
|
| 26 | +import org.springframework.web.util.UriComponents; |
| 27 | +import org.springframework.web.util.UriComponentsBuilder; |
26 | 28 |
|
27 | 29 | /**
|
28 | 30 | * Simple implementation of <tt>RedirectStrategy</tt> which is the default used throughout
|
29 | 31 | * the framework.
|
30 | 32 | *
|
31 | 33 | * @author Luke Taylor
|
| 34 | + * @author Josh Cummings |
32 | 35 | * @since 3.0
|
33 | 36 | */
|
34 | 37 | public class DefaultRedirectStrategy implements RedirectStrategy {
|
35 | 38 |
|
36 | 39 | protected final Log logger = LogFactory.getLog(getClass());
|
37 | 40 |
|
38 | 41 | private boolean contextRelative;
|
| 42 | + private boolean hostRelative = true; |
39 | 43 |
|
40 | 44 | /**
|
41 | 45 | * Redirects the response to the supplied URL.
|
@@ -68,25 +72,40 @@ protected String calculateRedirectUrl(String contextPath, String url) {
|
68 | 72 | }
|
69 | 73 |
|
70 | 74 | // Full URL, including http(s)://
|
| 75 | + boolean hostRelative = this.hostRelative; |
| 76 | + boolean contextRelative = isContextRelative(); |
71 | 77 |
|
72 |
| - if (!isContextRelative()) { |
| 78 | + if (!hostRelative && !contextRelative) { |
73 | 79 | return url;
|
74 | 80 | }
|
75 | 81 |
|
76 |
| - // Calculate the relative URL from the fully qualified URL, minus the last |
77 |
| - // occurrence of the scheme and base context. |
78 |
| - url = url.substring(url.lastIndexOf("://") + 3); // strip off scheme |
79 |
| - url = url.substring(url.indexOf(contextPath) + contextPath.length()); |
| 82 | + UriComponents components = UriComponentsBuilder |
| 83 | + .fromHttpUrl(url).build(); |
80 | 84 |
|
81 |
| - if (url.length() > 1 && url.charAt(0) == '/') { |
82 |
| - url = url.substring(1); |
| 85 | + String path = components.getPath(); |
| 86 | + if (contextRelative) { |
| 87 | + path = path.substring(path.indexOf(contextPath) + contextPath.length()); |
| 88 | + if (path.length() > 1 && path.charAt(0) == '/') { |
| 89 | + path = path.substring(1); |
| 90 | + } |
83 | 91 | }
|
84 | 92 |
|
85 |
| - return url; |
| 93 | + return UriComponentsBuilder |
| 94 | + .fromPath(path) |
| 95 | + .query(components.getQuery()) |
| 96 | + .build().toString(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * If <tt>true</tt>, causes any redirection URLs to be calculated minus the authority |
| 101 | + * (defaults to <tt>true</tt>). |
| 102 | + */ |
| 103 | + public void setHostRelative(boolean hostRelative) { |
| 104 | + this.hostRelative = hostRelative; |
86 | 105 | }
|
87 | 106 |
|
88 | 107 | /**
|
89 |
| - * If <tt>true</tt>, causes any redirection URLs to be calculated minus the protocol |
| 108 | + * If <tt>true</tt>, causes any redirection URLs to be calculated minus the authority |
90 | 109 | * and context path (defaults to <tt>false</tt>).
|
91 | 110 | */
|
92 | 111 | public void setContextRelative(boolean useRelativeContext) {
|
|
0 commit comments