From 0560bf4bd67e89a517d98d06567434fc063a8880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Haarl=C3=A4nder?= Date: Mon, 3 Jun 2024 13:14:16 +0200 Subject: [PATCH] Fixes for error redirection and query string --- .../presentation/rewrite/RequestHelper.java | 6 +++++- .../imeji/presentation/security/LoginBean.java | 16 ++++++++-------- .../presentation/security/SecurityFilter.java | 13 ++++++++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/mpg/imeji/presentation/rewrite/RequestHelper.java b/src/main/java/de/mpg/imeji/presentation/rewrite/RequestHelper.java index 580857d39..2110e71d0 100644 --- a/src/main/java/de/mpg/imeji/presentation/rewrite/RequestHelper.java +++ b/src/main/java/de/mpg/imeji/presentation/rewrite/RequestHelper.java @@ -24,7 +24,7 @@ public class RequestHelper { private final String contextPath; private final URI prettyRequestURL; private final URI originalRequestURL; - private final String requestQueryString; + private String requestQueryString; @@ -62,6 +62,10 @@ public class RequestHelper { } this.requestQueryString = request.getQueryString() != null ? request.getQueryString() : ""; + if(!requestQueryString.isEmpty() && !requestQueryString.startsWith("?")) + { + this.requestQueryString= "?" + requestQueryString; + } addParameters(requestQueryString); } diff --git a/src/main/java/de/mpg/imeji/presentation/security/LoginBean.java b/src/main/java/de/mpg/imeji/presentation/security/LoginBean.java index c874bc018..8e3c03ce1 100644 --- a/src/main/java/de/mpg/imeji/presentation/security/LoginBean.java +++ b/src/main/java/de/mpg/imeji/presentation/security/LoginBean.java @@ -135,15 +135,15 @@ private void redirectAfterLogin() { private void initRequestUrl() { //LOGGER.info("PrettyContext: " + PrettyContext.getCurrentInstance().getRequestURL().toURL()); - LOGGER.info("RequestHelper pretty: " + RequestHelper.getCurrentInstance().getPrettyRequestURL().toString()); - LOGGER.info("RequestHelper original: " + RequestHelper.getCurrentInstance().getOriginalRequestURL().toString()); - LOGGER.info( - "HttpRequestUri: " + ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURI()); - LOGGER.info("HttpRequestAttribute: " + ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()) - .getAttribute("javax.servlet.forward.request_uri")); + //LOGGER.info("RequestHelper pretty: " + RequestHelper.getCurrentInstance().getPrettyRequestURL().toString()); + //LOGGER.info("RequestHelper original: " + RequestHelper.getCurrentInstance().getOriginalRequestURL().toString()); + //LOGGER.info( + // "HttpRequestUri: " + ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURI()); + //LOGGER.info("HttpRequestAttribute: " + ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()) + // .getAttribute("javax.servlet.forward.request_uri")); //LOGGER.info("PrettyContext: " + PrettyContext.getCurrentInstance().getRequestQueryString().toQueryString()); - LOGGER.info(((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getQueryString()); - LOGGER.info("RequestHelper: " + RequestHelper.getCurrentInstance().getRequestQueryString()); + //LOGGER.info(((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getQueryString()); + //LOGGER.info("RequestHelper: " + RequestHelper.getCurrentInstance().getRequestQueryString()); this.requestUrl = getNavigation().getApplicationUri() + RequestHelper.getCurrentInstance().getPrettyRequestURL().toString() + RequestHelper.getCurrentInstance().getRequestQueryString(); diff --git a/src/main/java/de/mpg/imeji/presentation/security/SecurityFilter.java b/src/main/java/de/mpg/imeji/presentation/security/SecurityFilter.java index fe283c869..838d30729 100644 --- a/src/main/java/de/mpg/imeji/presentation/security/SecurityFilter.java +++ b/src/main/java/de/mpg/imeji/presentation/security/SecurityFilter.java @@ -90,16 +90,22 @@ public void doFilter(ServletRequest serv, ServletResponse resp, FilterChain chai } } catch (NotFoundException e) { ((HttpServletResponse) resp).sendError(Status.NOT_FOUND.getStatusCode(), "RESOURCE_NOT_FOUND"); + return; } catch (AuthenticationError e) { redirectToLoginPage(serv, resp); + return; } catch (NotAllowedError e) { ((HttpServletResponse) resp).sendError(Status.FORBIDDEN.getStatusCode(), "FORBIDDEN"); + return; } catch (Exception e) { LOGGER.error("Error in security Filter", e); ((HttpServletResponse) resp).sendError(Status.INTERNAL_SERVER_ERROR.getStatusCode(), "INTERNAL_SERVER_ERROR"); + return; } finally { - chain.doFilter(serv, resp); + } + chain.doFilter(serv, resp); + } /** @@ -220,8 +226,9 @@ private void redirectToLoginPage(ServletRequest serv, ServletResponse resp) thro HttpServletRequest request = (HttpServletRequest) serv; String url = NAVIGATION.getApplicationUri() + RequestHelper.getCurrentInstance(request).getPrettyRequestURL().toString(); Map> params = RequestHelper.getCurrentInstance(request).getRequestQueryParameters(); - ((HttpServletResponse) resp).sendRedirect(serv.getServletContext().getContextPath() + "/login?redirect=" - + URLEncoder.encode(url + HistoryUtil.paramsMapToString(params), "UTF-8")); + String urlToRedirect = serv.getServletContext().getContextPath() + "/login?redirect=" + + URLEncoder.encode(url + HistoryUtil.paramsMapToString(params), "UTF-8"); + ((HttpServletResponse) resp).sendRedirect(urlToRedirect); }