当Jetty服务器上的客户端身份验证失败时,浏览器上会出现黑屏

时间:2019-04-10 08:12:31

标签: java jetty

我已经使用嵌入式码头服务器创建了Java rest API,并启用了客户端身份验证。在客户端身份验证中,我获取客户端证书的CN,然后查看其是否被允许。 我创建了一个Filter类来处理上述客户端身份验证逻辑。但是,当客户端身份验证失败并返回过滤器时,将没有响应被发送回,并且在浏览器中点击该API的地方将出现黑屏。理想情况下,它应该响应一些错误。 我正在从已经添加了有效客户端证书的浏览器中访问API

在过滤器的响应对象中,我已经添加了响应的返回状态和其他一些东西,以使其终止请求,但没有用。

Enabling client auth which configuring SslContextFactory:

SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStore(keyStore);
sslContextFactory.setKeyStorePassword("changeit");
sslContextFactory.setKeyManagerPassword("changeit");
sslContextFactory.setCertAlias("selfsignlatest");
sslContextFactory.setNeedClientAuth(true);
sslContextFactory.setTrustStore(keyStore);
sslContextFactory.setTrustStorePassword("changeit");
Filter class doFilter method:

@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        logger.debug("Filtering incoming request....");
        boolean isValid = false;
        HttpServletRequest httpreq = (HttpServletRequest) request;
        HttpServletResponse httpresp = (HttpServletResponse) response;
        X509Certificate[] certificates = (X509Certificate[]) httpreq.getAttribute("javax.servlet.request.X509Certificate");
        if(certificates!=null && certificates.length>0) {
            for(X509Certificate cert: certificates) {
                commanName = getCNfromDomainName(cert.getSubjectX500Principal().getName());
                if(commanName!=null && commanName.equals("clientjetty")) {
                    isValid = true;
                    break;
                }
            }
        }

        if(isValid) {
            chain.doFilter(request, response);
        }else {
            httpresp.reset();
            httpresp.setHeader("Connection", "close");
            httpresp.setStatus(HttpStatus.SC_UNAUTHORIZED);
            httpresp.flushBuffer();
            httpresp.sendError(-1);
            return;
        }
    }

1 个答案:

答案 0 :(得分:0)

替换...

httpresp.reset();
httpresp.setHeader("Connection", "close");
httpresp.setStatus(HttpStatus.SC_UNAUTHORIZED);
httpresp.flushBuffer();
httpresp.sendError(-1);

有...

httpresp.setHeader("Connection", "close");
httpresp.sendError(HttpStatus.SC_UNAUTHORIZED);