我正在使用SSL(在NetBeans)一个非常简单的服务器上-要测试它,我已经创建了一个证书,这里的操作说明: http://java.sun.com/javaee/5/docs/tutorial/doc /bnbxw.html
java-home\bin\keytool -genkey -alias server-alias-keyalg RSA -keypass changeit -storepass changeit -keystore keystore.jks java-home\bin\keytool -export -alias server-alias -storepass changeit -file server.cer -keystore keystore.jks java-home\bin\keytool -import -v -trustcacerts -alias server-alias -file server.cer -keystore cacerts.jks -keypass changeit -storepass changeit
现在,使用SSL会得到一个简单的echo服务器,i'vre想尽了各种代码:所有这些都抛出大量的异常的 - 一个例子是如下:
try { System.setProperty("javax.net.ssl.keyStore", "cacerts.jks"); System.setProperty("javax.net.ssl.keyStorePassword", "changeit"); SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); ServerSocket ss = ssf.createServerSocket(5432); } catch (Exception e { e.printStackTrace(); }
这是最好的方式吗?
java页面提到了这个:
您必须输入服务器名称以响应keytool的第一个提示,在该提示中要求输入名和姓.出于测试目的,这可以是localhost.
运行示例应用程序时,密钥库中指定的主机必须与文件tut-install/javaeetutorial5/examples/bp-project/build.properties中指定的javaee.server.name属性中标识的主机匹配.
我如何将其应用于netbeans ide中的代码构建(和运行)?
更新:它会引发以下错误
java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl) at javax.net.ssl.DefaultSSLServerSocketFactory.throwException(SSLServerSocketFactory.java:142) at javax.net.ssl.DefaultSSLServerSocketFactory.createServerSocket(SSLServerSocketFactory.java:149) at MainClass.main(MainClass.java:23) Caused by: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl) at java.security.Provider$Service.newInstance(Provider.java:1245) at sun.security.jca.GetInstance.getInstance(GetInstance.java:220) at sun.security.jca.GetInstance.getInstance(GetInstance.java:147) at javax.net.ssl.SSLContext.getInstance(SSLContext.java:125) at javax.net.ssl.SSLContext.getDefault(SSLContext.java:68) at javax.net.ssl.SSLServerSocketFactory.getDefault(SSLServerSocketFactory.java:96) at MainClass.main(MainClass.java:21) Caused by: java.security.PrivilegedActionException: java.io.FileNotFoundException: cacerts.jks (The system cannot find the file specified) at java.security.AccessController.doPrivileged(Native Method) at com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl.getDefaultKeyManager(DefaultSSLContextImpl.java:120) at com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl.(DefaultSSLContextImpl.java:40) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at java.security.Provider$Service.newInstance(Provider.java:1221) ... 6 more Caused by: java.io.FileNotFoundException: cacerts.jks (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream. (FileInputStream.java:106) at java.io.FileInputStream. (FileInputStream.java:66) at com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl$2.run(DefaultSSLContextImpl.java:123) at com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl$2.run(DefaultSSLContextImpl.java:121) ... 16 more
非常感谢
如果您使用"localhost"作为自签名证书中的主机名,则HTTPS仅在您使用"localhost"作为请求的域名向服务器发送请求时才起作用.
编辑:基于异常,我会说真正的问题可能是您的密钥库文件不在JVM期望的位置.尝试在提供位置的系统属性中使用绝对路径名.