Skip to content

Commit 428b59e

Browse files
authored
Merge pull request #1727 from lprimak/redirect-follows-https
[#1762] enh: follow desired request scheme when doing redirection
2 parents 166bc01 + b964367 commit 428b59e

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.apache.shiro.session.SessionException;
5151
import org.apache.shiro.subject.Subject;
5252
import org.apache.shiro.subject.SubjectContext;
53+
import static org.apache.shiro.ee.listeners.EnvironmentLoaderListener.isShiroEERedirectDisabled;
5354
import static org.apache.shiro.web.filter.authz.SslFilter.HTTPS_SCHEME;
5455
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
5556
import org.apache.shiro.web.mgt.WebSecurityManager;
@@ -58,6 +59,7 @@
5859
import org.apache.shiro.web.subject.WebSubjectContext;
5960
import org.apache.shiro.web.util.WebUtils;
6061
import org.omnifaces.util.Servlets;
62+
import org.omnifaces.util.Utils;
6163

6264
/**
6365
* Stops JEE server from interpreting Shiro principal as direct EJB principal,
@@ -83,7 +85,7 @@ private static class WrappedRequest extends ShiroHttpServletRequest {
8385
@Getter(value = AccessLevel.PRIVATE, lazy = true)
8486
private final boolean httpsNeeded = createHttpButNeedHttps();
8587
@Getter(value = AccessLevel.PRIVATE, lazy = true)
86-
private final StringBuffer secureRequestURL = rewriteHttpToHttps();
88+
private final StringBuffer secureRequestURL = httpsRequestURL();
8789

8890
WrappedRequest(HttpServletRequest wrapped, ServletContext servletContext, boolean httpSessions) {
8991
super(wrapped, servletContext, httpSessions);
@@ -127,7 +129,7 @@ private boolean createHttpButNeedHttps() {
127129
.getHeader(X_FORWARDED_PROTO));
128130
}
129131

130-
private StringBuffer rewriteHttpToHttps() {
132+
private StringBuffer httpsRequestURL() {
131133
return new StringBuffer(HTTP_TO_HTTPS.matcher(super.getRequestURL())
132134
.replaceFirst(HTTPS_SCHEME + "$1"));
133135
}
@@ -147,6 +149,15 @@ public void addCookie(Cookie cookie) {
147149
super.addCookie(cookie);
148150
}
149151
}
152+
153+
@Override
154+
public void sendRedirect(String location) throws IOException {
155+
if (!Utils.startsWithOneOf(location, "http://", "https://")
156+
&& !isShiroEERedirectDisabled(request.getServletContext())) {
157+
location = Servlets.getRequestDomainURL(WebUtils.toHttp(request)) + location;
158+
}
159+
super.sendRedirect(location);
160+
}
150161
}
151162

152163
@RequiredArgsConstructor

support/jakarta-ee/src/main/java/org/apache/shiro/ee/listeners/EnvironmentLoaderListener.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
@WebListener
3434
public class EnvironmentLoaderListener extends EnvironmentLoader implements ServletContextListener {
3535
private static final String SHIRO_EE_DISABLED_PARAM = "org.apache.shiro.ee.disabled";
36+
private static final String SHIRO_EE_REDIRECT_DISABLED_PARAM = "org.apache.shiro.ee.redirect.disabled";
3637
private static final String FORM_RESUBMIT_DISABLED_PARAM = "org.apache.shiro.form-resubmit.disabled";
3738
private static final String FORM_RESUBMIT_SECURE_COOKIES = "org.apache.shiro.form-resubmit.secure-cookies";
3839
private static final String SHIRO_WEB_DISABLE_PRINCIPAL_PARAM = "org.apache.shiro.web.disable-principal";
@@ -41,6 +42,10 @@ public static boolean isShiroEEDisabled(ServletContext ctx) {
4142
return Boolean.TRUE.equals(ctx.getAttribute(SHIRO_EE_DISABLED_PARAM));
4243
}
4344

45+
public static boolean isShiroEERedirectDisabled(ServletContext ctx) {
46+
return Boolean.TRUE.equals(ctx.getAttribute(SHIRO_EE_REDIRECT_DISABLED_PARAM));
47+
}
48+
4449
public static boolean isFormResubmitDisabled(ServletContext ctx) {
4550
return Boolean.TRUE.equals(ctx.getAttribute(FORM_RESUBMIT_DISABLED_PARAM));
4651
}
@@ -58,6 +63,9 @@ public void contextInitialized(ServletContextEvent sce) {
5863
if (Boolean.parseBoolean(sce.getServletContext().getInitParameter(SHIRO_EE_DISABLED_PARAM))) {
5964
sce.getServletContext().setAttribute(SHIRO_EE_DISABLED_PARAM, Boolean.TRUE);
6065
}
66+
if (Boolean.parseBoolean(sce.getServletContext().getInitParameter(SHIRO_EE_REDIRECT_DISABLED_PARAM))) {
67+
sce.getServletContext().setAttribute(SHIRO_EE_REDIRECT_DISABLED_PARAM, Boolean.TRUE);
68+
}
6169
if (Boolean.parseBoolean(sce.getServletContext().getInitParameter(FORM_RESUBMIT_DISABLED_PARAM))) {
6270
sce.getServletContext().setAttribute(FORM_RESUBMIT_DISABLED_PARAM, Boolean.TRUE);
6371
}

0 commit comments

Comments
 (0)