forked from openmrs/openmrs-module-legacyui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LUI-170 - Not redirecting to requested page after redirected to login… (
openmrs#130) * LUI-170 - Not redirecting to requested page after redirected to login page This adds a new request filter that sets the redirect url on the session to the currently requested url if the user is not authenticated and it is not already set. * LUI-170 - Not redirecting to requested page after redirected to login page Follow-up tweaks following code review.
- Loading branch information
Showing
2 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
omod/src/main/java/org/openmrs/module/web/filter/RedirectAfterLoginFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.web.filter; | ||
|
||
import static org.openmrs.web.WebConstants.OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
import java.io.IOException; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.openmrs.api.context.Context; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This filter serves to set the redirect url on the session, if any GET request is received for an | ||
* unauthenticated user, so that any downstream redirection after login can bring the user to the | ||
* page they had been requesting. | ||
*/ | ||
public class RedirectAfterLoginFilter implements Filter { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(RedirectAfterLoginFilter.class); | ||
|
||
private FilterConfig config; | ||
|
||
/** | ||
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) | ||
*/ | ||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | ||
try { | ||
HttpServletRequest httpReq = (HttpServletRequest) req; | ||
String requestURI = httpReq.getRequestURI(); | ||
if (!Context.isAuthenticated()) { | ||
if ("GET".equalsIgnoreCase(httpReq.getMethod()) && !requestURI.contains("login.")) { | ||
HttpSession session = httpReq.getSession(false); | ||
if (session != null && session.getAttribute(OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR) == null) { | ||
String queryParams = httpReq.getQueryString(); | ||
String redirectUrl = requestURI + (StringUtils.isNotBlank(queryParams) ? "?" + queryParams : ""); | ||
httpReq.getSession().setAttribute(OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR, redirectUrl); | ||
if (log.isDebugEnabled()) { | ||
log.debug("Set {} = {}", OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR, redirectUrl); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception e) { | ||
log.warn("An error occurred while setting session attribute " + OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR, e); | ||
} | ||
chain.doFilter(req, res); | ||
} | ||
|
||
/** | ||
* @see Filter#init(FilterConfig) | ||
*/ | ||
public void init(FilterConfig config) throws ServletException { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* @see Filter#destroy() | ||
*/ | ||
public void destroy() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters