0

我有一個簡單的HTTP服務和JSON負載,我想用Java測試工具進行測試。 最初我使用Basic Auth設置了一個可以正常工作的客戶端;服務器證書位於trustStore中,我在代碼中提供用戶名/密碼。我發送請求,我得到正確的迴應。使用HttpURLConnection設置相互身份驗證客戶端

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    String xxxURL = new String("https://www.xxx.yyy/zzz/AdminServlet?data="); 
    String username = new String("username"); 
    String password = new String("password"); 
    String authString = new String (username+":"+password); 

    String apiList = new String("{\"apiVersion\":\"1.4\",\"method\":\"api.list\",\"params\":{}}"); // Create JSON string. A bit ugly 

    try 
    { 

     System.setProperty("javax.net.ssl.trustStore","C:\\workspace\\http_client_test\\security\\cacerts"); 



    String jsonStr = apiList; 
     URL url = new URL(xxxURL + URLEncoder.encode(jsonStr, "UTF-8")); 

     HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); // create connection object 
     String encoded = Base64.encodeBase64String(authString.getBytes()); 
     httpConn.setRequestProperty("Authorization", "Basic "+encoded); 

     httpConn.setRequestMethod("GET"); 
     httpConn.connect();   // open connection 

     BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); 
     String temp = null; 
     StringBuilder sb = new StringBuilder(); 
     while((temp = in.readLine()) != null) 
     { 
      sb.append(temp).append(" "); 
     } 

     String result = sb.toString(); 
     System.out.println("result = " + result); 

    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 


} 

我想用相互認證來做同樣的測試。我已經在服務器和客戶機上設置了密鑰庫和信任庫,並在每個證書上導入了必要的證書。

我的問題是我找不出如何告訴HttpURLConnection,我想要相互證書認證。

我想: 公共類Test3的{

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    String xxxURL = new String("https://www.xxx.yyy/zzz/AdminServlet?data="); 

    String apiList = new String("{\"apiVersion\":\"1.4\",\"method\":\"api.list\",\"params\":{}}"); // Create JSON string. A bit ugly 

    try 
    { 

     System.setProperty("javax.net.ssl.trustStore","C:\\workspace\\http_client_test\\security\\cacerts"); 
     System.setProperty("javax.net.ssl.trustStorePassword","changeit"); 
     System.setProperty("javax.net.ssl.keyStore","C:\\workspace\\http_client_test\\security\\keystore.jks"); 
     System.setProperty("javax.net.ssl.keyStorePassword","password"); 




     String jsonStr = apiList; 
     URL url = new URL(netThingsURL + URLEncoder.encode(jsonStr, "UTF-8")); 

     HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); // create connection object 
     String encoded = Base64.encodeBase64String(authString.getBytes()); 
     httpConn.setRequestProperty("Authorization", "?????????");  //  ???????? 

     httpConn.setRequestMethod("GET"); 
     httpConn.connect();   // open connection 

     BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); 
     String temp = null; 
     StringBuilder sb = new StringBuilder(); 
     while((temp = in.readLine()) != null) 
     { 
      sb.append(temp).append(" "); 
     } 

     String result = sb.toString(); 
     System.out.println("result = " + result); 

    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 


} 

}

我應該在這裏:httpConn.setRequestProperty( 「授權」, 「?????????」) ;我意識到我可能需要的不僅僅是這一行。我嘗試了各種資源來找到適當的值,但畫了一個空白。我嘗試了各種直觀的參數,但得到'401'錯誤或NoSuchAlgorithException。

任何幫助,代碼,資源鏈接,非常感謝。

回答

1

我的問題是我找不出如何告訴HttpURLConnection,我想要相互證書認證。

你不行。您必須配置服務器以請求客戶端證書。您可以在客戶端執行的所有操作都是指定客戶端證書的位置。你不能強迫它被髮送。只有服務器可以做到這一點。

相關問題