@Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        ThreadContext.unbindSubject();
        final boolean secure = requestContext.getSecurityContext().isSecure();
        final MultivaluedMap<String, String> headers = requestContext.getHeaders();
        final Map<String, Cookie> cookies = requestContext.getCookies();
        final Request grizzlyRequest = grizzlyRequestProvider.get();

        final String host = RestTools.getRemoteAddrFromRequest(grizzlyRequest, trustedProxies);
        final String authHeader = headers.getFirst(HttpHeaders.AUTHORIZATION);
        final Set<Class<?>> matchedResources = requestContext.getUriInfo().getMatchedResources().stream()
                .map(Object::getClass).collect(Collectors.toSet());

        final SecurityContext securityContext;
        if (authHeader != null && authHeader.startsWith("Basic")) {
            final String base64UserPass = authHeader.substring(authHeader.indexOf(' ') + 1);
            final String userPass = decodeBase64(base64UserPass);
            final String[] split = userPass.split(":", 2);

            if (split.length != 2) {
                throw new BadRequestException("Invalid credentials in Authorization header");
            }

            securityContext = createSecurityContext(split[0],
                    split[1],
                    secure,
                    SecurityContext.BASIC_AUTH,
                    host,
                    grizzlyRequest.getRemoteAddr(),
                    headers,
                    cookies,
                    matchedResources);

        } else {
            securityContext = createSecurityContext(null, null, secure, null, host,
                    grizzlyRequest.getRemoteAddr(),
                    headers,
                    cookies,
                    matchedResources);
        }

        requestContext.setSecurityContext(securityContext);
    }
@Test(expected = BadRequestException.class)
    public void filterWithBasicAuthAndMalformedCredentialsShouldThrowBadRequestException() throws Exception {
        final MultivaluedHashMap<String, String> headers = new MultivaluedHashMap<>();
        final String credentials = Base64.getEncoder().encodeToString("user_pass".getBytes(StandardCharsets.US_ASCII));
        headers.putSingle(HttpHeaders.AUTHORIZATION, "Basic " + credentials);
        when(requestContext.getHeaders()).thenReturn(headers);

        filter.filter(requestContext);
    }