Skip to content

Commit

Permalink
LUI-170 - Not redirecting to requested page after redirected to login… (
Browse files Browse the repository at this point in the history
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
mseaton committed Dec 9, 2020
1 parent 1130e48 commit d09aedb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
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() {
}
}
15 changes: 9 additions & 6 deletions omod/src/main/java/org/openmrs/web/WebComponentRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
*/
package org.openmrs.web;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.util.EnumSet;

import org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener;
import org.openmrs.module.web.filter.AdminPageFilter;
import org.openmrs.module.web.filter.ForcePasswordChangeFilter;
import org.openmrs.web.servlet.LogoutServlet;
import org.openmrs.module.web.filter.RedirectAfterLoginFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

Expand All @@ -30,13 +29,17 @@ public class WebComponentRegistrar implements ServletContextAware {
public void setServletContext(ServletContext servletContext) {

try {
String[] mappings = { "*.htm", "*.form", "*.list", "*.json", "*.field", "*.portlet", "*.page", "*.action" };

ServletRegistration openmrsServletReg = servletContext.getServletRegistration("openmrs");
addMappings(openmrsServletReg, "*.htm", "*.form", "*.list", "*.json", "*.field", "*.portlet", "*.page",
"*.action");
addMappings(openmrsServletReg, mappings);

addMappings(servletContext.getServletRegistration("jsp"), "*.withjstl");

Dynamic filter = servletContext.addFilter("forcePasswordChangeFilter", new ForcePasswordChangeFilter());
Dynamic filter = servletContext.addFilter("redirectAfterLoginFilter", new RedirectAfterLoginFilter());
filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, mappings);

filter = servletContext.addFilter("forcePasswordChangeFilter", new ForcePasswordChangeFilter());
filter.setInitParameter("changePasswordForm", "/admin/users/changePassword.form");
filter.setInitParameter("excludeURL", "changePasswordForm,logout,.js,.css,.gif,.jpg,.jpeg,.png");
filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
Expand Down

0 comments on commit d09aedb

Please sign in to comment.