设置HTTPS连接的https.protocols系统属性的问题

nam*_*olk 6 java ssl https

我有一个Java实现,各种客户端应用程序使用它来连接到第三方系统。这些第三方系统通过http / https支持不同的协议。在这种情况下,所有客户端应用程序都托管在Java实现所在的同一服务器上。因此,在这种情况下,各种客户端应用程序会为系统属性设置各种https协议(例如:System.setProperty("https.protocols", "SSLv3")System.setProperty("https.protocols", "TLS")当它们使用此协议连接到那些第三方系统时)。

在这里,系统属性在该环境中的所有应用程序之间共享。因此,修改System属性会导致许多问题。所以,我想知道

  1. 有没有办法在不使用系统属性的情况下完成此任务?
  2. 有没有办法设置所有可能的https.protocols,使其支持与第三方系统建立的任何http或https连接,从而支持各种协议?

blogs.oracle.com中提到的每个JDK版本都支持的协议和算法: 在此处输入图片说明

代码:

String responseStr = null;

System.setProperty("https.protocols",http-protocol); // This is set by the client applications. Previously, there was one by one (eg : "SSLv3". Then I changed it to "TLSv1.2,TLSv1.1,TLSv1,SSLv3" assuming it will enable all) 

byteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(requestStr.getBytes());

URL mUrl = new URL(proxy_url);
HttpURLConnection con = (HttpURLConnection) mUrl.openConnection(); // It works fine for the HttpURLConnection when there's no (s)

con.setRequestMethod("POST");
con.setDoOutput(true);
con.setUseCaches(false);
con.setDoInput(true);

con.setRequestProperty("user-agent","Mozilla(MSIE)");
con.setRequestProperty("Accept-Encoding","gzip,deflate");

byteArrayOutputStream.writeTo(con.getOutputStream());

String encodingHeader = con.getHeaderField("Content-Encoding");
InputStream inputStream = null;

if(encodingHeader != null && encodingHeader.toLowerCase().indexOf("gzip") != -1){
    inputStream = new GZIPInputStream(con.getInputStream());
}else {
    inputStream = con.getInputStream();

}

if (inputStream != null) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
       byte[] buffer = new byte[4096];
       int length = 0;

       while ((length = inputStream.read(buffer)) != -1) {
           baos.write(buffer, 0, length);
       }

    responseStr = new String(baos.toByteArray());
    baos.close();

 }
Run Code Online (Sandbox Code Playgroud)

我的Java版本:1.5

use*_*421 0

我建议你根本不要设置它。只需让系统与对等方协商即可。它将协商最强的共享协议。

  • 我明白你在说什么。但请考虑这样一种情况:我有一个名为“x”的客户端应用程序和另一个使用我的实现的名为“y”的客户端应用程序。假设我的java版本是1.7。应用程序 x 需要 TLSv1.2 来连接第三方,因此,当没有提及协议时它不会连接,因为 JAVA 1.7 的默认值为 TLSv1。当我们将 http.protocols 设置为 TLSv1.2 时,它就可以工作。但它会影响需要其他协议进行连接的其他客户端应用程序“y”。如果我们不提及 https.protocol 它会使用默认协议,对吧?不是最好的。 (2认同)