LBHttpsEnforcerInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { final String forwardedHeader = request.getHeader(X_FORWARDED_PROTO_HEADER); if (StringUtils.isBlank(forwardedHeader) || !"HTTPS".equalsIgnoreCase(forwardedHeader)) { LOG.debug("redirecting non-https request with header: {}", forwardedHeader); response.sendRedirect(applicationUrl); return false; } return true; }  @Override boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler); static final String X_FORWARDED_PROTO_HEADER; }
@Test public void shouldRedirectIfNoHeaderWasSet() throws Exception { boolean shouldContinue = lbHttpsEnforcerInterceptor.preHandle(request, response, null); assertThat(shouldContinue, is(false)); verify(response).sendRedirect(APPLICATION_URL); }
@Test public void shouldRedirectIfHeaderWasEmpty() throws Exception { request.addHeader(LBHttpsEnforcerInterceptor.X_FORWARDED_PROTO_HEADER, ""); boolean shouldContinue = lbHttpsEnforcerInterceptor.preHandle(request, response, null); assertThat(shouldContinue, is(false)); verify(response).sendRedirect(APPLICATION_URL); }
@Test public void shouldNotRedirectIfHeaderWasHttps() throws Exception { request.addHeader(LBHttpsEnforcerInterceptor.X_FORWARDED_PROTO_HEADER, "HTTPS"); boolean shouldContinue = lbHttpsEnforcerInterceptor.preHandle(request, response, null); assertThat(shouldContinue, is(true)); verify(response, never()).sendRedirect(any()); }