diff --git a/hawtio-system/src/main/java/io/hawt/web/auth/Redirector.java b/hawtio-system/src/main/java/io/hawt/web/auth/Redirector.java index a62df9be9f..74a19c3758 100644 --- a/hawtio-system/src/main/java/io/hawt/web/auth/Redirector.java +++ b/hawtio-system/src/main/java/io/hawt/web/auth/Redirector.java @@ -27,7 +27,13 @@ public void doRedirect(HttpServletRequest request, HttpServletResponse response, scheme = request.getServletContext().getInitParameter("scheme"); } if (scheme != null) { - redirectUrl = scheme + "://" + request.getServerName() + ":" + request.getServerPort(); + int port = request.getServerPort(); + if (port == 80 || port == 443) { + // default ports should not be added to redirect URL + redirectUrl = String.format("%s://%s", scheme, request.getServerName()); + } else { + redirectUrl = String.format("%s://%s:%s", scheme, request.getServerName(), port); + } } redirectUrl += request.getContextPath() + applicationContextPath + path; diff --git a/hawtio-system/src/test/java/io/hawt/web/auth/RedirectorTest.java b/hawtio-system/src/test/java/io/hawt/web/auth/RedirectorTest.java index eb7667ce64..78eb2e3bc8 100644 --- a/hawtio-system/src/test/java/io/hawt/web/auth/RedirectorTest.java +++ b/hawtio-system/src/test/java/io/hawt/web/auth/RedirectorTest.java @@ -26,7 +26,6 @@ public void setUp() { servletContext = mock(ServletContext.class); when(request.getServerName()).thenReturn("server01"); - when(request.getServerPort()).thenReturn(9000); when(request.getContextPath()).thenReturn("/context-path"); when(request.getServletContext()).thenReturn(servletContext); } @@ -42,6 +41,7 @@ public void shouldRedirectToRelativeUrlByDefault() throws Exception { redirector = new Redirector(); redirector.setApplicationContextPath("/application-context-path"); when(servletContext.getInitParameter("scheme")).thenReturn(null); + when(request.getServerPort()).thenReturn(9000); // when redirector.doRedirect(request, response, "/path"); // then @@ -54,6 +54,7 @@ public void shouldRedirectToAbsoluteUrlWhenSchemeIsConfigured1() throws Exceptio redirector = new Redirector(); redirector.setApplicationContextPath("/application-context-path"); when(servletContext.getInitParameter("scheme")).thenReturn("https"); + when(request.getServerPort()).thenReturn(9000); // when redirector.doRedirect(request, response, "/path"); // then @@ -66,10 +67,37 @@ public void shouldRedirectToAbsoluteUrlWhenSchemeIsConfigured2() throws Exceptio System.setProperty(Redirector.HAWTIO_REDIRECT_SCHEME, "https"); redirector = new Redirector(); redirector.setApplicationContextPath("/application-context-path"); + when(request.getServerPort()).thenReturn(9000); // when redirector.doRedirect(request, response, "/path"); // then verify(response).sendRedirect("https://server01:9000/context-path/application-context-path/path"); } + @Test + public void shouldRedirectToAbsoluteUrlWhenSchemeIsConfiguredPort80() throws Exception { + // given + System.setProperty(Redirector.HAWTIO_REDIRECT_SCHEME, "http"); + redirector = new Redirector(); + redirector.setApplicationContextPath("/application-context-path"); + when(request.getServerPort()).thenReturn(80); + // when + redirector.doRedirect(request, response, "/path"); + // then + verify(response).sendRedirect("http://server01/context-path/application-context-path/path"); + } + + @Test + public void shouldRedirectToAbsoluteUrlWhenSchemeIsConfiguredPort443() throws Exception { + // given + System.setProperty(Redirector.HAWTIO_REDIRECT_SCHEME, "https"); + redirector = new Redirector(); + redirector.setApplicationContextPath("/application-context-path"); + when(request.getServerPort()).thenReturn(443); + // when + redirector.doRedirect(request, response, "/path"); + // then + verify(response).sendRedirect("https://server01/context-path/application-context-path/path"); + } + }