java ssl多个客户端证书

时间:2016-08-25 15:41:41

标签: java ssl jersey x509 jersey-1.0

我的服务器配置为接受由私有CA签名的任何客户端证书,但似乎无法让多个客户端使用不同的私钥进行连接以进行测试。

我正在尝试创建一个单元测试,与admin连接以设置测试,然后断开连接,重新连接,因为有限用户在断开连接之前执行操作,然后再次使用admin打开以验证有限用户操作是否成功。

只与一个客户端连接完全正常,问题是我的服务器日志显示管理员记录所有3次,因为正如wireshark确认的那样,ssl会话只是使用缩短的会话恢复握手而不是重新协商每个连接。

有没有办法迫使球衣客户重新协商每个连接?

客户端测试代码:

System.setProperty("javax.net.ssl.keyStore", "admin.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
makeClient().resource(getResetURI()).post(Response.class, RESET_DATA);
System.setProperty("javax.net.ssl.keyStore", "limited-user.jks");
makeClient().resource(getUpdateURI()).post(Response.class, TEST_UPDATE_SMALL_DATA); // this connection is actually as admin

...
    public Client makeClient() {
        final DefaultApacheHttpClientConfig clientConfig = new DefaultApacheHttpClientConfig();
        clientConfig.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true);    
        clientConfig.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
        String keystore = Util.getSystemProperty("javax.net.ssl.keyStore");;
        String keystorePassword = Util.getSystemProperty("javax.net.ssl.keyStorePassword");
        try{
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            KeyStore ks = KeyStore.getInstance("JKS");
            try(FileInputStream fis = new java.io.FileInputStream(keystore)){
                ks.load(fis, keystorePassword.toCharArray());
            }
            kmf.init(ks, keystorePassword.toCharArray());
            SSLContext sslctx = SSLContext.getInstance("TLSv1.2");
            sslctx.getClientSessionContext().setSessionCacheSize(1);
            sslctx.getClientSessionContext().setSessionTimeout(1);
            // TODO: the below two nulls are probably insecure
            sslctx.init(kmf.getKeyManagers(), null, new SecureRandom());
            clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(null, sslctx));
        }
        catch(Exception e)
        {
            throw new RuntimeException(e);
        }

        /* we must construct our own handler in order to override the Retry handler */
        final ApacheHttpClientHandler clientHandler = new ApacheHttpClientHandler(new HttpClient(new MultiThreadedHttpConnectionManager()), clientConfig);
        clientHandler.getHttpClient().getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));

    Client tclient = new ApacheHttpClient(clientHandler);
    tclient.setFollowRedirects(true);
    return tclient;
}

我也尝试在调用之间重置ssl会话,但就我所见,这没有任何影响:

SSLSessionContext ssc = SSLContext.getInstance("TLSv1.2").getClientSessionContext();
for (Enumeration<byte[]> e = ssc.getIds(); e.hasMoreElements();)
{
   byte[] id = e.nextElement();
   ssc.getSession(id).invalidate();
   System.out.println("Invalidating!");
}
Thread.sleep(1100);

是否可以仅在一个JVM中更改证书,还是必须为此测试生成多个JVM?

1 个答案:

答案 0 :(得分:0)

Apache HTTP Client不支持在单个JVM中使用多个证书。但是,原始的Jersey客户端确实支持每个实例的证书,并且切换到它应该启用每个实例的配置。请注意,Apache HTTP Client通常用于它的cookie管理,因此如果您需要,您将不得不在Jersey客户端上重新实现cookie管理(请参阅Jersey Client: Adding Cookies to Request以获得简单的实现)