@Test public void testThatAuthShouldNotHappenForAnonymousPUTEndpointsOnNoRequestInfo() throws IOException { RequestContext ctx = RequestContext.getCurrentContext(); request.setRequestURI("anonymous-endpoint1"); request.setMethod("PUT"); request.setContent(IOUtils.toByteArray(IOUtils.toInputStream("{\"ServiceRequest\": {\"fu\": \"bar\"}}"))); ctx.setRequest(request); authPreCheckFilter.run(); assertFalse((Boolean) ctx.get("shouldDoAuth")); request.setRequestURI("anonymous-endpoint1"); ctx.setRequest(request); authPreCheckFilter.run(); assertFalse((Boolean) ctx.get("shouldDoAuth")); }
@Override public Object run() { String authToken; if (openEndpointsWhitelist.contains(getRequestURI())) { setShouldDoAuth(false); logger.info(OPEN_ENDPOINT_MESSAGE, getRequestURI()); return null; } try { authToken = getAuthTokenFromRequest(); } catch (IOException e) { logger.error(AUTH_TOKEN_RETRIEVE_FAILURE_MESSAGE, e); ExceptionUtils.RaiseException(e); return null; } RequestContext.getCurrentContext().set(AUTH_TOKEN_KEY, authToken); if (authToken == null) { if (mixedModeEndpointsWhitelist.contains(getRequestURI())) { logger.info(ROUTING_TO_ANONYMOUS_ENDPOINT_MESSAGE, getRequestURI()); setShouldDoAuth(false); } else { logger.info(ROUTING_TO_PROTECTED_ENDPOINT_RESTRICTED_MESSAGE, getRequestURI()); ExceptionUtils.raiseCustomException(HttpStatus.UNAUTHORIZED, UNAUTHORIZED_USER_MESSAGE); return null; } } else { logger.info(PROCEED_ROUTING_MESSAGE, getRequestURI()); setShouldDoAuth(true); } return null; }
AuthPreCheckFilter extends ZuulFilter { @Override public Object run() { String authToken; if (openEndpointsWhitelist.contains(getRequestURI())) { setShouldDoAuth(false); logger.info(OPEN_ENDPOINT_MESSAGE, getRequestURI()); return null; } try { authToken = getAuthTokenFromRequest(); } catch (IOException e) { logger.error(AUTH_TOKEN_RETRIEVE_FAILURE_MESSAGE, e); ExceptionUtils.RaiseException(e); return null; } RequestContext.getCurrentContext().set(AUTH_TOKEN_KEY, authToken); if (authToken == null) { if (mixedModeEndpointsWhitelist.contains(getRequestURI())) { logger.info(ROUTING_TO_ANONYMOUS_ENDPOINT_MESSAGE, getRequestURI()); setShouldDoAuth(false); } else { logger.info(ROUTING_TO_PROTECTED_ENDPOINT_RESTRICTED_MESSAGE, getRequestURI()); ExceptionUtils.raiseCustomException(HttpStatus.UNAUTHORIZED, UNAUTHORIZED_USER_MESSAGE); return null; } } else { logger.info(PROCEED_ROUTING_MESSAGE, getRequestURI()); setShouldDoAuth(true); } return null; } }
AuthPreCheckFilter extends ZuulFilter { @Override public Object run() { String authToken; if (openEndpointsWhitelist.contains(getRequestURI())) { setShouldDoAuth(false); logger.info(OPEN_ENDPOINT_MESSAGE, getRequestURI()); return null; } try { authToken = getAuthTokenFromRequest(); } catch (IOException e) { logger.error(AUTH_TOKEN_RETRIEVE_FAILURE_MESSAGE, e); ExceptionUtils.RaiseException(e); return null; } RequestContext.getCurrentContext().set(AUTH_TOKEN_KEY, authToken); if (authToken == null) { if (mixedModeEndpointsWhitelist.contains(getRequestURI())) { logger.info(ROUTING_TO_ANONYMOUS_ENDPOINT_MESSAGE, getRequestURI()); setShouldDoAuth(false); } else { logger.info(ROUTING_TO_PROTECTED_ENDPOINT_RESTRICTED_MESSAGE, getRequestURI()); ExceptionUtils.raiseCustomException(HttpStatus.UNAUTHORIZED, UNAUTHORIZED_USER_MESSAGE); return null; } } else { logger.info(PROCEED_ROUTING_MESSAGE, getRequestURI()); setShouldDoAuth(true); } return null; } AuthPreCheckFilter(HashSet<String> openEndpointsWhitelist,
                              HashSet<String> mixedModeEndpointsWhitelist); }
AuthPreCheckFilter extends ZuulFilter { @Override public Object run() { String authToken; if (openEndpointsWhitelist.contains(getRequestURI())) { setShouldDoAuth(false); logger.info(OPEN_ENDPOINT_MESSAGE, getRequestURI()); return null; } try { authToken = getAuthTokenFromRequest(); } catch (IOException e) { logger.error(AUTH_TOKEN_RETRIEVE_FAILURE_MESSAGE, e); ExceptionUtils.RaiseException(e); return null; } RequestContext.getCurrentContext().set(AUTH_TOKEN_KEY, authToken); if (authToken == null) { if (mixedModeEndpointsWhitelist.contains(getRequestURI())) { logger.info(ROUTING_TO_ANONYMOUS_ENDPOINT_MESSAGE, getRequestURI()); setShouldDoAuth(false); } else { logger.info(ROUTING_TO_PROTECTED_ENDPOINT_RESTRICTED_MESSAGE, getRequestURI()); ExceptionUtils.raiseCustomException(HttpStatus.UNAUTHORIZED, UNAUTHORIZED_USER_MESSAGE); return null; } } else { logger.info(PROCEED_ROUTING_MESSAGE, getRequestURI()); setShouldDoAuth(true); } return null; } AuthPreCheckFilter(HashSet<String> openEndpointsWhitelist,
                              HashSet<String> mixedModeEndpointsWhitelist); @Override String filterType(); @Override int filterOrder(); @Override boolean shouldFilter(); @Override Object run(); }
AuthPreCheckFilter extends ZuulFilter { @Override public Object run() { String authToken; if (openEndpointsWhitelist.contains(getRequestURI())) { setShouldDoAuth(false); logger.info(OPEN_ENDPOINT_MESSAGE, getRequestURI()); return null; } try { authToken = getAuthTokenFromRequest(); } catch (IOException e) { logger.error(AUTH_TOKEN_RETRIEVE_FAILURE_MESSAGE, e); ExceptionUtils.RaiseException(e); return null; } RequestContext.getCurrentContext().set(AUTH_TOKEN_KEY, authToken); if (authToken == null) { if (mixedModeEndpointsWhitelist.contains(getRequestURI())) { logger.info(ROUTING_TO_ANONYMOUS_ENDPOINT_MESSAGE, getRequestURI()); setShouldDoAuth(false); } else { logger.info(ROUTING_TO_PROTECTED_ENDPOINT_RESTRICTED_MESSAGE, getRequestURI()); ExceptionUtils.raiseCustomException(HttpStatus.UNAUTHORIZED, UNAUTHORIZED_USER_MESSAGE); return null; } } else { logger.info(PROCEED_ROUTING_MESSAGE, getRequestURI()); setShouldDoAuth(true); } return null; } AuthPreCheckFilter(HashSet<String> openEndpointsWhitelist,
                              HashSet<String> mixedModeEndpointsWhitelist); @Override String filterType(); @Override int filterOrder(); @Override boolean shouldFilter(); @Override Object run();  }