Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当curl-E成功时,JavaSSL连接得到403_Java_Ssl_Https_Jks - Fatal编程技术网

当curl-E成功时,JavaSSL连接得到403

当curl-E成功时,JavaSSL连接得到403,java,ssl,https,jks,Java,Ssl,Https,Jks,这个问题类似于,但我似乎无法从这个问题中得到建议的解决方案。我过去可以连接到.Net Web服务,但现在他们通过证书实现了SLL,我无法再访问它。服务器团队向我提供了一个证书,使我能够再次连接到Web服务。我已经确认这是使用curl工作的: curl -E cer_file.pem:password https://hostname/servicename.svc?wsdl 因此,上面正确返回了wsdl。我必须创建.cacert.pem文件才能使curl调用正常工作,以避免出现以下消息(cur

这个问题类似于,但我似乎无法从这个问题中得到建议的解决方案。我过去可以连接到.Net Web服务,但现在他们通过证书实现了SLL,我无法再访问它。服务器团队向我提供了一个证书,使我能够再次连接到Web服务。我已经确认这是使用curl工作的:

curl -E cer_file.pem:password https://hostname/servicename.svc?wsdl
因此,上面正确返回了wsdl。我必须创建.cacert.pem文件才能使curl调用正常工作,以避免出现以下消息(curl:(77)设置证书验证位置时出错):

现在,当我尝试使用下面的Java时,我仍然得到403。我想知道是否有人能发现我做错了什么

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;


public class TestSSL {

private static final String WS_URL 
    = "https://hostname/servicename.svc?wsdl";
private static final String KS_FILE 
    = "C:/tstfolder/client_side_keystore.jks";
private static final String TS_FILE 
    = "C:/tstfolder/curl_trusted_ca.jks";

public static void main(String... args) throws Exception {
    testConn();
}

public static void testConn() throws Exception {
    System.setProperty("javax.net.ssl.keyStore", KS_FILE);
    System.setProperty("javax.net.ssl.keyStoreType", "jks");
    System.setProperty("javax.net.ssl.keyStorePassword", "importkey");
    System.setProperty("javax.net.ssl.trustStoreType", "jks");
    System.setProperty("javax.net.ssl.trustStore", TS_FILE);
    System.setProperty("javax.net.ssl.trustStorePassword", "password");
    System.setProperty("javax.net.debug", "all");

    SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    URL url = new URL(WS_URL);
    HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
    conn.setSSLSocketFactory(sslsocketfactory);

    InputStream inputstream = conn.getInputStream();
    InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
    BufferedReader bufferedreader = new BufferedReader(inputstreamreader);


    String string = null;
    while ((string = bufferedreader.readLine()) != null) {
        System.out.println("Received " + string);
    }
}
}
下面是我用来创建信任库和密钥库的步骤。将cer_file.pem拆分为单独的cert.pem和key.pem。然后使用下面链接中的说明,我创建了client_side_keystore.jks。然后,我使用在中找到的实用程序将curl certs文件导入到jks中,从而创建了信任存储


不幸的是,当我尝试通过java测试类读取wsdl时,我得到了403。当它通过curl调用工作时。我怀疑我在做一些愚蠢的事情,或者是误解了什么。但是我想知道是否有人能为我指出正确的方向?

听起来你的密钥库没有正确转换

直接从OpenSSL构建PKCS#12文件可能更容易:

openssl pkcs12 -export -in file.crt -inkey file.key -out file.p12
然后,直接使用
PKCS12
作为密钥库类型(或者使用
keytool
将其转换为JKS)


如前所述,如果有中级证书,您可能需要检查链是否正确。

谢谢,您发布的链接对我有用。似乎我的客户证书没有被发送,因为它没有链接到请求的签名权限。
openssl pkcs12 -export -in file.crt -inkey file.key -out file.p12