我一直试图在Cpp服务器和Java小程序客户端之间建立连接,但是当我运行小程序时,在浏览器中出现此错误。我在信任库文件中有证书(如果不需要,请告诉我)。任何帮助,将不胜感激。


  访问被拒绝(“ java.util.propertypermission”
  “ javax.net.ssl.truststore”“写”)


import java.awt.*;
import java.applet.*;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import javax.swing.*;

public class no2 extends Applet {

   int width, height;

   public void init() {

        System.setProperty("javax.net.ssl.trustStore", "keystore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

        String trustStore = System.getProperty("javax.net.ssl.trustStore");
        if (trustStore == null) {
            System.out.println("javax.net.ssl.trustStore is not defined");
        } else {
            System.out.println("javax.net.ssl.trustStore = " + trustStore);
        }

        try {

        PrintWriter toServer = null;
        BufferedReader fromServer = null;

        SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("192.168.0.10", 12120);

        String[] newProtocols = {"TLSv1"};
        sslsocket.setEnabledProtocols(newProtocols);

        toServer = new PrintWriter(sslsocket.getOutputStream(), true);

        toServer.println("Data1\n");
        toServer.println("Data2\n");

        InputStreamReader isr = new InputStreamReader(sslsocket.getInputStream());
        fromServer = new BufferedReader(isr, 1);

        } catch (Exception exception) {

            exception.printStackTrace();
        }
   }
}

最佳答案

您无法在Applet中设置系统属性。您也不能访问本地文件(keystore.jks)系统(除非您请求这样做的特定权限)。

您可以像documentation中所述将特殊参数传递给JVM,如下所示:

<APPLET archive="my_applet.jar" code="MyApplet" width="300" height="300">
    <PARAM name="java_arguments" value="-Djavax.net.ssl.trustStore=keystore.jks">
</APPLET>


但同样,请注意访问本地文件所需的特殊权限。

关于java - 谁能告诉我为什么我在使用SSL时出现Java错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11376323/

10-11 17:09