@Test public void testThatFilterShouldAbortIfValidatingAuthTokenFails() throws IOException { RequestContext ctx = RequestContext.getCurrentContext(); String authToken = "dummy-auth-token"; ctx.set("authToken", authToken); request.setMethod("POST"); ctx.setRequest(request); ctx.setResponse(new MockHttpServletResponse()); String authUrl = String.format("%s%s%s", authServiceHost, authUri, authToken); when(restTemplate.postForObject(eq(authUrl), any(), eq(User.class))) .thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED)); try { authFilter.run(); assertFalse("Shouldn't reach here", true ); } catch (RuntimeException ex) { assertThat(((HttpClientErrorException)ex.getCause()).getStatusCode().value(), is(401)); } }
@Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); String authToken = (String) ctx.get(AUTH_TOKEN_KEY); try { User user = getUser(authToken, ctx); ctx.set(USER_INFO_KEY, user); } catch (HttpClientErrorException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.RaiseException(ex); } catch (ResourceAccessException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.raiseCustomException(HttpStatus.INTERNAL_SERVER_ERROR, "User authentication service is down"); } return null; }
AuthFilter extends ZuulFilter { @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); String authToken = (String) ctx.get(AUTH_TOKEN_KEY); try { User user = getUser(authToken, ctx); ctx.set(USER_INFO_KEY, user); } catch (HttpClientErrorException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.RaiseException(ex); } catch (ResourceAccessException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.raiseCustomException(HttpStatus.INTERNAL_SERVER_ERROR, "User authentication service is down"); } return null; } }
AuthFilter extends ZuulFilter { @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); String authToken = (String) ctx.get(AUTH_TOKEN_KEY); try { User user = getUser(authToken, ctx); ctx.set(USER_INFO_KEY, user); } catch (HttpClientErrorException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.RaiseException(ex); } catch (ResourceAccessException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.raiseCustomException(HttpStatus.INTERNAL_SERVER_ERROR, "User authentication service is down"); } return null; } AuthFilter(ProxyRequestHelper helper, RestTemplate restTemplate, String authServiceHost, String authUri); }
AuthFilter extends ZuulFilter { @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); String authToken = (String) ctx.get(AUTH_TOKEN_KEY); try { User user = getUser(authToken, ctx); ctx.set(USER_INFO_KEY, user); } catch (HttpClientErrorException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.RaiseException(ex); } catch (ResourceAccessException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.raiseCustomException(HttpStatus.INTERNAL_SERVER_ERROR, "User authentication service is down"); } return null; } AuthFilter(ProxyRequestHelper helper, RestTemplate restTemplate, String authServiceHost, String authUri); @Override String filterType(); @Override int filterOrder(); @Override boolean shouldFilter(); @Override Object run(); }
AuthFilter extends ZuulFilter { @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); String authToken = (String) ctx.get(AUTH_TOKEN_KEY); try { User user = getUser(authToken, ctx); ctx.set(USER_INFO_KEY, user); } catch (HttpClientErrorException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.RaiseException(ex); } catch (ResourceAccessException ex) { logger.error(RETRIEVING_USER_FAILED_MESSAGE, ex); ExceptionUtils.raiseCustomException(HttpStatus.INTERNAL_SERVER_ERROR, "User authentication service is down"); } return null; } AuthFilter(ProxyRequestHelper helper, RestTemplate restTemplate, String authServiceHost, String authUri); @Override String filterType(); @Override int filterOrder(); @Override boolean shouldFilter(); @Override Object run();  }