Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.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
Java Android HttpsURLConnection在模拟器上使用自签名证书,但在实际设备上不使用_Java_Android_Amazon Ec2_Httpsurlconnection_Self Signed Certificate - Fatal编程技术网

Java Android HttpsURLConnection在模拟器上使用自签名证书,但在实际设备上不使用

Java Android HttpsURLConnection在模拟器上使用自签名证书,但在实际设备上不使用,java,android,amazon-ec2,httpsurlconnection,self-signed-certificate,Java,Android,Amazon Ec2,Httpsurlconnection,Self Signed Certificate,我使用此命令为运行在EC2实例上的nginx服务器创建自签名证书 sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/selfsigned.key -out /etc/ssl/certs/selfsigned.crt 作为一个公共名称(例如服务器FQDN或您的名称),我使用了EC2实例的公共DNS,类似于 ec2-somenumber.region.compute.amazonaws

我使用此命令为运行在EC2实例上的nginx服务器创建自签名证书

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/selfsigned.key -out /etc/ssl/certs/selfsigned.crt
作为一个公共名称(例如服务器FQDN或您的名称),我使用了EC2实例的公共DNS,类似于 ec2-somenumber.region.compute.amazonaws.com

我使用此代码来解决信任问题, 我将selfsigned.crt复制到应用程序原始文件夹,并以以下方式使用它:

CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = getResources().openRawResource(R.raw.selfsigned);
Certificate ca = cf.generateCertificate(caInput);

// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

// Create an SSLContext that uses our TrustManager
SSLContext _sslContext = SSLContext.getInstance("TLS");
_sslContext.init(null, tmf.getTrustManagers(), null);

URL url = new URL("https://ec2-somenumber.region.compute.amazonaws.com");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setSSLSocketFactory(_sslContext.getSocketFactory());
现在它可以在emulator上完美地工作,但当我尝试在真实设备上调试它时,它会给我以下错误:

 javax.net.ssl.SSLPeerUnverifiedException: Hostname ec2-somenumber.region.compute.amazonaws.com not verified
我在stackoverlow上读了很多问题,实际上我不想覆盖hostnameVerifier,直到我理解为什么它可以在emulator上工作,但不能在真正的设备上工作

你有什么建议吗


谢谢

如果您有相同的问题,请参阅此以生成证书

要使自签名证书在真实设备上工作,我需要在用于创建证书的ssl.conf文件中指定主题替代名称,如下面所示

[alt_names]
DNS.1   = ec2-somenumber.region.compute.amazonaws.com

如果您有相同的问题,请参阅此以生成证书

要使自签名证书在真实设备上工作,我需要在用于创建证书的ssl.conf文件中指定主题替代名称,如下面所示

[alt_names]
DNS.1   = ec2-somenumber.region.compute.amazonaws.com