我对sslConnectionin java有一个问题,我在下面编写了客户端代码(这个应用程序必须连接到服务器,我有支持ssl的服务器)但我得到这个错误“javax.net.ssl.SSLException:Connection has已关闭:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径“
如何解决我的问题?
public static void main(String[] args) {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
PrintStream out = System.out;
SSLSocketFactory f =
(SSLSocketFactory) SSLSocketFactory.getDefault();
try {
SSLSocket c =
(SSLSocket) f.createSocket("192.168.10.38", 7701);
printSocketInfo(c);
System.out.println("End printSocketInfo");
c.startHandshake();
System.out.println("HandShake oK");
BufferedWriter w = new BufferedWriter(
new OutputStreamWriter(c.getOutputStream()));
BufferedReader r = new BufferedReader(
new InputStreamReader(c.getInputStream()));
String m = null;
// String m=
while ((m=r.readLine())!= null) {
System.out.println("11111");
out.println(m);
m = in.readLine();
System.out.println("M is: "+ m);
w.write(m,0,m.length());
w.newLine();
w.flush();
}
w.close();
r.close();
c.close();
} catch (IOException e) {
System.err.println(e.toString());
}
}
private static void printSocketInfo(SSLSocket s) {
System.out.println("Socket class: "+s.getClass());
System.out.println(" Remote address = "
+s.getInetAddress().toString());
System.out.println(" Remote port = "+s.getPort());
System.out.println(" Local socket address = "
+s.getLocalSocketAddress().toString());
System.out.println(" Local address = "
+s.getLocalAddress().toString());
System.out.println(" Local port = "+s.getLocalPort());
System.out.println(" Need client authentication = "
+s.getNeedClientAuth());
SSLSession ss = s.getSession();
System.out.println(" Cipher suite = "+ss.getCipherSuite());
System.out.println(" Protocol = "+ss.getProtocol());
}
我有证书文件,如何使用此证书?
最好的问候
答案 0 :(得分:1)
您必须将HTTPS证书放入JVM。要从HTTPS获取证书,请浏览浏览器,然后单击地址栏上的“锁定”徽标。您应该能够导出证书。
Linux解决方案:在$ JAVA_HOME / jre / lib / security中,使用以下命令:
sudo keytool -import -alias keyName -file /[pathForYourKey]/keyName.cert -keystore cacerts
“cacerts”的默认密码是changeit。
答案 1 :(得分:1)
是自签名证书
如果是,那么您有两种选择 第一选择:
在Global Java Certificate Trust store中导入证书颁发机构证书。这家商店位于
%Java Installation%/jre/lib/security/cacerts
要导入它,您可以使用Java安装附带的Keytool命令
keytool -import -alias keyName -file yourcertificate.crt -keystore cacerts
第二个选项:
如果您想绕过证书验证,请按照Java: Overriding function to disable SSL certificate check
进行操作否则 为您的程序创建新的Trust商店
keytool -import -alias keyName -file yourcertificate.crt -keystore yourtruststore
此命令将要求输入密码两次。输入您想要的任何密码并输入"是"对于任何问题 将在当前目录中按名称" yourtruststore"
创建一个文件现在您需要在程序中使用此信任库
SSLSocketFactory sslFactory = null;
InputStream trustStore = null;
KeyStore keyStore = null;
trustStore = new FileInputStream("<your trust store absolute path>");
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(trustStore, "<your trust store password>".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
sslFactory = ctx.getSocketFactory();
您可以使用此套接字工厂打开新套接字