2016-04-26 95 views
2

我一直在試圖讓我的球衣客戶端使用我的Jersey/Grizzly Rest api做一個ssl客戶端驗證。其他客戶端與此服務器握手成功,但我在使用Jersey客戶端的Java客戶端時遇到問題。當我運行下面的代碼時,密鑰庫已成功加載,並且在調用SslConfigurator的createSSLContext()時,ssl調試輸出顯示正確訪問此密鑰庫並找到我的私鑰。jersey-client 2.22.2 - 如何正確設置SslConfigurator上的SunPKCS11密鑰庫?

但是,當使用客戶端的WebTarget時,ssl調試輸出顯示使用默認密鑰庫JKS發生握手。爲什麼ClientBuilder不使用SSLContext中的這個密鑰庫?

File tmpConfigFile = File.createTempFile("pkcs11-", "conf"); 
     tmpConfigFile.deleteOnExit(); 
     PrintWriter configWriter = new PrintWriter(new FileOutputStream(tmpConfigFile), true); 
     configWriter.println("name=ActiveClient"); 
     configWriter.println("library=\"C:\\\\Program Files\\\\ActivIdentity\\\\ActivClient\\\\acpkcs211.dll\""); 
     configWriter.println("slotListIndex=0"); 
     SunPKCS11 provider = new SunPKCS11(tmpConfigFile.getAbsolutePath()); 
     Security.addProvider(provider); 
     // KeyStore keyStore = KeyStore.getInstance("PKCS11", provider); 

     KeyStore keyStore = KeyStore.getInstance("PKCS11"); 
     keyStore.load(null, null); 

     ClientConfig config = new ClientConfig(); 

     SslConfigurator sslConfig = SslConfigurator.newInstance() 
       .keyStore(keyStore) 
       .keyStorePassword("mypin") 
       .keyStoreType("PKCS11") 
       .trustStoreFile(TRUSTORE_CLIENT_FILE) 
       .trustStorePassword(TRUSTSTORE_CLIENT_PWD) 
       .securityProtocol("TLS"); 



     final SSLContext sslContext = sslConfig.createSSLContext(); 

     Client client = ClientBuilder 
       .newBuilder().hostnameVerifier(new MyHostnNameVerifier()) 
       .sslContext(sslContext) 
       .build(); 
     WebTarget target = client.target("https://localhost:8443/appname/resources/employees?qparam=something"); 
    Response res = target.request().accept(MediaType.APPLICATION_JSON).get(); 

回答

0

此代碼實際工作。問題在於我的服務器的信任證書不適用於需要信任的智能卡證書。我將正確的證書添加到服務器上的信任庫,然後運行。 ssl調試消息不是很清楚。

0

這次我遇到了很多問題,我找到了實現我的目標的方法。在你的例子中,我看不到任何使用ClientConfig config實例。這爲我工作:

ClientConfig config = new ClientConfig(); 
    config.connectorProvider(new ApacheConnectorProvider()); 
    Client client = ClientBuilder.newBuilder().hostnameVerifier(new MyHostnNameVerifier()) 
      .sslContext(sslContext).withConfig(config).build(); 

我發現ApacheConnectorProvider更適合使用安全層或代理(女巫是另一個巨大的問題我解決)連接。