gpt4 book ai didi

android - 自签名证书 - 未找到信任 anchor

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:33 25 4
gpt4 key购买 nike

编辑:评论中的 BNK 已链接到找到的解决方案 here .

我正在通过 REST 将 POST 请求发送到后端服务器(通过 LAN),所有这些都是通过 HTTPS 完成的。此服务器有一个自签名证书作为 .pem 文件,一切正常。

我现在正在尝试连接到不同的 Web 服务器(通过 WAN,通过 DNS),一个自签名证书也是一个 .crt 文件(标准,BER/DER 格式)。但是现在,尽管代码相同,但我收到以下异常:

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

我不确定为什么一台服务器可以连接但另一台服务器不行。我不想信任所有证书,因为这将通过公共(public)互联网进行。

我的网络代码:

public HttpsURLConnection setUpHttpsConnection(String urlString)
{
try
{
// Load CAs from an InputStream
CertificateFactory cf = CertificateFactory.getInstance("X.509");

InputStream caInput = new BufferedInputStream(context.getAssets().open("server.crt"));
Certificate ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((java.security.cert.X509Certificate) ca).getSubjectDN());

// 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);

// Create all-trusting host name verifier
// to avoid the following :
// java.security.cert.CertificateException: No name matching
// This is because Java by default verifies that the certificate CN (Common Name) is
// the same as host name in the URL. If they are not, the web service client fails.
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
// Install it
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

// Tell the URLConnection to use a SocketFactory from our SSLContext
URL url = new URL(urlString);
HttpsURLConnection urlConnection = null;
urlConnection = (HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(sslContext.getSocketFactory());

return urlConnection;
}
catch (Exception ex)
{
Log.e("NetworkManager", "Failed to establish SSL connection to server: " + ex.toString());
return null;
}
}

/**
* Represents an asynchronous login/registration task used to authenticate
* the user.
*/
public class POSTTask extends AsyncTask<POSTRequest, Void, StringBuilder>
{
POSTTask()
{
}

@Override
protected void onPreExecute() {}

@Override
protected StringBuilder doInBackground(POSTRequest... params)
{
OutputStream os = null;

try {
HttpsURLConnection urlConnection = setUpHttpsConnection(params[0].url);
//Sets the maximum time to wait for an input stream read to complete before giving up.
urlConnection.setReadTimeout(30000);
//Sets the maximum time in milliseconds to wait while connecting.
urlConnection.setConnectTimeout(20000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params[0].nameValuePairs);
os = urlConnection.getOutputStream();
formEntity.writeTo(os);

InputStream in = urlConnection.getInputStream();
StringBuilder ret = inputStreamToString(in);

return ret;

} catch (IOException e) {
Log.i("NetworkError", e.toString());
} catch (Exception e) {

} finally {
if (os != null) {
try {
os.close();
} catch (IOException ex) {
}
}
}
return null;
}

@Override
protected void onPostExecute(StringBuilder result) {
}

@Override
protected void onCancelled() {
}
}

最佳答案

如果我正确理解你关于“all tr​​usting”的想法,也就是你代码中的hostname verifier,你可以引用以下内容:

假设您的服务器应用托管在 IIS 中,IIS 中有一个服务器证书,例如,"Issued to""localhost"。然后,在验证方法中,您可以验证 "localhost"

HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier hv =
HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify("localhost", session);
}
};

关于android - 自签名证书 - 未找到信任 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32304014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com