简体   繁体   English

错误的Java连接HTTPS和SSL证书

[英]Error Java connection HTTPS with SSL certificates

I have a problem to connect with SSL in Java. 我在使用Java连接SSL时遇到问题。

I use java 1.7.80 and SO : ubuntu 14.04 我使用Java 1.7.80和SO:Ubuntu 14.04

My problem is the certificate, even certificates. 我的问题是证书,甚至证书。 I have four elements that must be concatenated using the pkcs12 format. 我有四个必须使用pkcs12格式连接的元素。

testservizi.fattura.it.cer --> certificate that identifies the remote server testservizi.fattura.it.cer >标识远程服务器的证书

SDI-11036210158 --> certificate client SDI-11036210158 >证书客户端

CAEntratetest.cer --> certificate CA CAEntratetest.cer >证书CA

private.key --> private key private.key >私钥

1) Step - convert the file cer into file pem 1)步骤-将文件cer转换为文件pem

$ openssl x509 -in CAEntratetest.cer -outform PEM -out CAEntratetest.pem
$ openssl x509 -in SDI-11036210158.cer -inform DER -out SDI-11036210158.pem -outform PEM
$ openssl x509 -in testservizi.fatturapa.it.cer -inform DER -out testservizi.fatturapa.it.pem -outform PEM

2) Step - union pem files for create chain certificate 2)步骤-联合pem文件以创建链证书

$ cat CAEntratetest.pem SDI-11036210158.pem testservizi.fatturapa.it.pem > sum_cert.pem

3) Step - create p12 file 3)步骤-创建p12文件

$ openssl pkcs12 -export -in sum_cert.pem -inkey private.key -out sogei_auth.p12 -name sogei_auth

Ok. 好。 My first test is to import the p12 file in the browser (Mozilla Firefox) to verify the operation. 我的第一个测试是在浏览器(Mozilla Firefox)中导入p12文件以验证操作。 The import is successful and at this point I enter the url 导入成功,此时我输入网址

https://testservizi.fatturapa.it/ricevi_file https://testservizi.fatturapa.it/ricevi_file

and he answered: 他回答:

Hello! 你好! This is an Axis2 Web Service! 这是Axis2 Web服务!

..perfect works!! ..完美的作品!!

Now I have to make it work with java and I create a test client 现在,我必须使其与Java一起使用,并创建一个测试客户端

import java.io.File;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

public class TestHTTPSClient {

    public static void main(String[] args) throws Exception {
        SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new File("C:/testSSL/sogei_auth.p12"), "changeit".toCharArray()).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        try {
            HttpGet httpget = new HttpGet("https://testservizi.fatturapa.it/ricevi_file");
            System.out.println("executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
}

2) Change format keystore in java.security , otherwise you can not read the file p12 2)更改java.security中的格式密钥库,否则您将无法读取文件p12

keystore.type=jks

change into 变成

keystore.type=pkcs12

3) Start testing in java and I answered with an error 3)开始在Java中测试,我回答了一个错误

Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
...
..
.
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
... 26 more

can you help me... 你能帮助我吗...

you may store in jre cacerts file for permanently solve this issue if runtime certification stuff is not required! 如果不需要运行时认证,您可以将其存储在jre cacerts文件中以永久解决此问题! Check this answer for installing certificate to cacerts... 检查答案以将证书安装到cacerts ...

and remove all certification related code form your source! 并从您的来源中删除所有与认证相关的代码! Please clarify if you want to do this runtime only not manually. 请说明您是否仅希望不手动执行此运行时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM