BasicAuthRequestFilter implements HttpRequestFilter { @Override public Request filterRequest(Request request) { if (!requestMatcher.test(request)) { return request; } return fromRequest(request) .addHeader(AUTHORIZATION, authenticationHeader) .build(); } BasicAuthRequestFilter(String user, String password);  BasicAuthRequestFilter(Predicate<Request> requestMatcher, String user, String password); @Override Request filterRequest(Request request);  }
@Test public void testBasicAuthentication() { Predicate<Request> predicate = request -> request.getUri().getPath().startsWith("/private"); HttpRequestFilter filter = new BasicAuthRequestFilter(predicate, "Aladdin", "open sesame"); Request publicResourceRequest = createTestRequest("/public"); assertNull(filter.filterRequest(publicResourceRequest).getHeader(AUTHORIZATION)); Request privateResourceRequest = createTestRequest("/private"); assertEquals(filter.filterRequest(privateResourceRequest).getHeader(AUTHORIZATION), "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); }