gpt4 book ai didi

java - 我在使用自签名证书时收到 SSLHandshakeException 和 CertPathValidatorException 错误

转载 作者:行者123 更新时间:2023-12-02 11:19:26 25 4
gpt4 key购买 nike

我正在尝试向 apache2 发送 https 请求,随后 Django 收到该请求。该代码在使用 ssl 和 http 之前工作正常,但现在出现异常。我只需要通过apache2向django发送https请求。我的 apache2 服务器在 AWS 服务器上运行。

这位于我的 MainActivity.java 文件中。

这是我的 hurlstack 代码

 hurlStack = new HurlStack() {

@Override
protected HttpsURLConnection createConnection(URL url) throws IOException {
Log.w("testing","in hurlstack exception ");

HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url);
Log.w("testing","after url ");

try {
Log.w("testing","in hurlstack try ");
httpsURLConnection.setSSLSocketFactory(getSSLSocketFactory());
httpsURLConnection.setHostnameVerifier(getHostnameVerifier());
} catch (Exception e) {
e.printStackTrace();
Log.w("testing","In hurlstack exception"+e.toString());
}
Log.w("testing","end of hurlstack" + httpsURLConnection);
return httpsURLConnection;
}
};

private HostnameVerifier getHostnameVerifier() {
Log.w("testing","In hostname verifier");
return new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
//return true; // verify always returns true, which could cause insecure network traffic due to trusting TLS/SSL server certificates for wrong hostnames
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify(hostname, session);
}
};
}

private TrustManager[] getWrappedTrustManagers(TrustManager[] trustManagers) {
Log.w("testing","In trust manager");
final X509TrustManager originalTrustManager = (X509TrustManager) trustManagers[0];
return new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return originalTrustManager.getAcceptedIssuers();
}

public void checkClientTrusted(X509Certificate[] certs, String authType) {
try {
if (certs != null && certs.length > 0){
certs[0].checkValidity();
} else {
originalTrustManager.checkClientTrusted(certs, authType);
}
} catch (CertificateException e) {
Log.w("testing", "certificate error "+e.toString());
}
}

public void checkServerTrusted(X509Certificate[] certs, String authType) {
try {
if (certs != null && certs.length > 0){
certs[0].checkValidity();
} else {
originalTrustManager.checkServerTrusted(certs, authType);
}
} catch (CertificateException e) {
Log.w("testing", "certificate server error "+e.toString());
}
}
}
};
}

private SSLSocketFactory getSSLSocketFactory()
throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {
Log.w("testing", " in ssl socket factory");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = getResources().openRawResource(R.raw.apache_selfsigned); // this is the file with .crt extension stored in \app\src\main\res\raw folder path
Log.w("testing", "ssl socket");
Certificate ca = cf.generateCertificate(caInput);
caInput.close();

KeyStore keyStore = KeyStore.getInstance("BKS");
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers());

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, wrappedTrustManagers, null);

return sslContext.getSocketFactory();
}

我有一个 MySingleton.java 文件

private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();

}

public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}

public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext(),new HurlStack());
}
return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {

// getRequestQueue().getCache().clear();
req.setRetryPolicy(new DefaultRetryPolicy(60000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
getRequestQueue().add(req);
}

我用来创建自签名证书的命令是

sudo openssl req -new -x509 -nodes -out apache-selfsigned.crt -keyout apache-selfsigned.key

最佳答案

您需要获取上面生成的证书请求文件并对其进行自签名以生成证书文件。

openssl x509 -trustout -signkey apache-selfsigned.key -days 365 -req -in apache-selfsigned.crt -out apache-selfsigned.cer

关于java - 我在使用自签名证书时收到 SSLHandshakeException 和 CertPathValidatorException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50038448/

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