Skip to content

Commit b84c542

Browse files
committed
enh: follow desired request scheme when doing redirection
1 parent b3fe8f6 commit b84c542

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

support/jakarta-ee/src/main/java/org/apache/shiro/ee/filters/FormAuthenticationFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, Ser
8181
ServletResponse response) throws Exception {
8282
if (request instanceof HttpServletRequest) {
8383
FallbackPredicate loginFallbackType = (FallbackPredicate) request.getAttribute(LOGIN_PREDICATE_ATTR_NAME);
84-
redirectToSaved(WebUtils.toHttp(request), WebUtils.toHttp(response), loginFallbackType, "");
84+
redirectToSaved(WebUtils.toHttp(request), WebUtils.toHttp(response), loginFallbackType, "/");
8585
}
8686
return false;
8787
}

support/jakarta-ee/src/main/java/org/apache/shiro/ee/filters/Forms.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public boolean isLoggedIn() {
9393
}
9494

9595
public boolean redirectIfLoggedIn() {
96-
return redirectIfLoggedIn("");
96+
return redirectIfLoggedIn("/");
9797
}
9898

9999
public boolean redirectIfLoggedIn(String view) {
@@ -155,7 +155,7 @@ public static void redirectToView(FallbackPredicate useFallbackPath, String fall
155155
public static void login(String username, String password, boolean rememberMe) {
156156
try {
157157
SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password, rememberMe));
158-
redirectToSaved(Faces.getRequestAttribute(LOGIN_PREDICATE_ATTR_NAME), "");
158+
redirectToSaved(Faces.getRequestAttribute(LOGIN_PREDICATE_ATTR_NAME), "/");
159159
} catch (AuthenticationException e) {
160160
Faces.setFlashAttribute(DEFAULT_ERROR_KEY_ATTRIBUTE_NAME, e);
161161
int loginFailedWaitTime = Faces.getRequestAttribute(LOGIN_WAITTIME_ATTR_NAME);

support/jakarta-ee/src/main/java/org/apache/shiro/ee/filters/ShiroFilter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.apache.shiro.web.subject.WebSubjectContext;
5959
import org.apache.shiro.web.util.WebUtils;
6060
import org.omnifaces.util.Servlets;
61+
import org.omnifaces.util.Utils;
6162

6263
/**
6364
* Stops JEE server from interpreting Shiro principal as direct EJB principal,
@@ -83,7 +84,7 @@ private static class WrappedRequest extends ShiroHttpServletRequest {
8384
@Getter(value = AccessLevel.PRIVATE, lazy = true)
8485
private final boolean httpsNeeded = createHttpButNeedHttps();
8586
@Getter(value = AccessLevel.PRIVATE, lazy = true)
86-
private final StringBuffer secureRequestURL = rewriteHttpToHttps();
87+
private final StringBuffer secureRequestURL = httpsRequestURL();
8788

8889
WrappedRequest(HttpServletRequest wrapped, ServletContext servletContext, boolean httpSessions) {
8990
super(wrapped, servletContext, httpSessions);
@@ -127,7 +128,7 @@ private boolean createHttpButNeedHttps() {
127128
.getHeader(X_FORWARDED_PROTO));
128129
}
129130

130-
private StringBuffer rewriteHttpToHttps() {
131+
private StringBuffer httpsRequestURL() {
131132
return new StringBuffer(HTTP_TO_HTTPS.matcher(super.getRequestURL())
132133
.replaceFirst(HTTPS_SCHEME + "$1"));
133134
}
@@ -147,6 +148,14 @@ public void addCookie(Cookie cookie) {
147148
super.addCookie(cookie);
148149
}
149150
}
151+
152+
@Override
153+
public void sendRedirect(String location) throws IOException {
154+
if (!Utils.startsWithOneOf(location, new String[]{"http://", "https://"})) {
155+
location = Servlets.getRequestDomainURL(WebUtils.toHttp(request)) + location;
156+
}
157+
super.sendRedirect(location);
158+
}
150159
}
151160

152161
@RequiredArgsConstructor

0 commit comments

Comments
 (0)