@Test public void shouldRejectRequestsWithoutCredentials() throws IOException, ServletException { new BasicAuthFilter("testuser", "testpass").doFilter(request, response, chain); verify(response).setHeader(Matchers.eq(HttpHeaders.WWW_AUTHENTICATE), Matchers.contains("Basic")); verify(response).sendError(401); }
@Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String header = request.getHeader(HttpHeaders.AUTHORIZATION); if (header != null && header.startsWith("Basic ")) { String decoded = new String(BaseEncoding.base64().decode(header.substring(header.indexOf(" ") + 1))); if (decoded.contains(":")) { String username = decoded.substring(0, decoded.indexOf(":")); String password = decoded.substring(decoded.indexOf(":") + 1, decoded.length()); if (username.equals(this.username) && password.equals(this.password)) { chain.doFilter(request, response); return; } else { log.info("Incorrect admin login with username '{}'.", username); } } } response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Administration\""); response.sendError(Response.SC_UNAUTHORIZED); }
BasicAuthFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String header = request.getHeader(HttpHeaders.AUTHORIZATION); if (header != null && header.startsWith("Basic ")) { String decoded = new String(BaseEncoding.base64().decode(header.substring(header.indexOf(" ") + 1))); if (decoded.contains(":")) { String username = decoded.substring(0, decoded.indexOf(":")); String password = decoded.substring(decoded.indexOf(":") + 1, decoded.length()); if (username.equals(this.username) && password.equals(this.password)) { chain.doFilter(request, response); return; } else { log.info("Incorrect admin login with username '{}'.", username); } } } response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Administration\""); response.sendError(Response.SC_UNAUTHORIZED); } }
BasicAuthFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String header = request.getHeader(HttpHeaders.AUTHORIZATION); if (header != null && header.startsWith("Basic ")) { String decoded = new String(BaseEncoding.base64().decode(header.substring(header.indexOf(" ") + 1))); if (decoded.contains(":")) { String username = decoded.substring(0, decoded.indexOf(":")); String password = decoded.substring(decoded.indexOf(":") + 1, decoded.length()); if (username.equals(this.username) && password.equals(this.password)) { chain.doFilter(request, response); return; } else { log.info("Incorrect admin login with username '{}'.", username); } } } response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Administration\""); response.sendError(Response.SC_UNAUTHORIZED); }  }
BasicAuthFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String header = request.getHeader(HttpHeaders.AUTHORIZATION); if (header != null && header.startsWith("Basic ")) { String decoded = new String(BaseEncoding.base64().decode(header.substring(header.indexOf(" ") + 1))); if (decoded.contains(":")) { String username = decoded.substring(0, decoded.indexOf(":")); String password = decoded.substring(decoded.indexOf(":") + 1, decoded.length()); if (username.equals(this.username) && password.equals(this.password)) { chain.doFilter(request, response); return; } else { log.info("Incorrect admin login with username '{}'.", username); } } } response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Administration\""); response.sendError(Response.SC_UNAUTHORIZED); }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest req, ServletResponse res, FilterChain chain); @Override void destroy(); static void addToAdmin(Environment env, String username, String password); }
BasicAuthFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String header = request.getHeader(HttpHeaders.AUTHORIZATION); if (header != null && header.startsWith("Basic ")) { String decoded = new String(BaseEncoding.base64().decode(header.substring(header.indexOf(" ") + 1))); if (decoded.contains(":")) { String username = decoded.substring(0, decoded.indexOf(":")); String password = decoded.substring(decoded.indexOf(":") + 1, decoded.length()); if (username.equals(this.username) && password.equals(this.password)) { chain.doFilter(request, response); return; } else { log.info("Incorrect admin login with username '{}'.", username); } } } response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Administration\""); response.sendError(Response.SC_UNAUTHORIZED); }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest req, ServletResponse res, FilterChain chain); @Override void destroy(); static void addToAdmin(Environment env, String username, String password);  }