簡體   English   中英

忽略Servlet中的SSL證書

[英]Ignore SSL Certificate in a Servlet

我收到以下異常:

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

我做了一些研究並改變了我的連接代碼:

SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();

CloseableHttpClient client = HttpClients.custom()
            .setRedirectStrategy(new LaxRedirectStrategy()) 
            .setSslcontext(sslContext)   
            .setConnectionManager(connMgr)
            .build();

這解決了到目前為止的問題,我不再獲得異常並且連接正常。

當我在Tomcat中運行的Servlet中使用相同的代碼時,問題再次出現。

為什么?

請嘗試以下代碼。

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;

public class DummyX509TrustManager implements X509TrustManager {

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    @Override
    public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString)
            throws CertificateException {
    }

    @Override
    public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString)
            throws CertificateException {
    }
};


final TrustManager[] trustAllCerts = new TrustManager[] { new DummyX509TrustManager() };
try {
    SSLContext sslContext= SSLContext.getInstance("SSL"); 
    sslContext.init(null, trustAllCerts, null);

    CloseableHttpClient client = HttpClients.custom()
        .setRedirectStrategy(new LaxRedirectStrategy()) 
        .setSslcontext(sslContext)   
        .setConnectionManager(connMgr)
        .build();
} catch (KeyManagementException e) {
    throw new IOException(e.getMessage());
} catch (NoSuchAlgorithmException e) {
    throw new IOException(e.getMessage());
}

是由私人/公司擁有的CA頒發的網站證書還是自簽名的?

... new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() ...
  • Trustore為null:
    您是否嘗試加載包含受信任CA及其鏈的密鑰庫/文件?
  • isTrusted總是返回“true”:
    您正在覆蓋標准的JSSE證書驗證流程並信任所有流程,因此根本沒有安全性。
  • 該例外:表示證書驗證失敗。 RootCA或整個CA鏈缺失。

所以看起來Tomcat忽略了你的SSLContext 像這樣使用,無論如何sslContext都是無用的。 調試結果? JVM SSL設置? 異常堆棧跟蹤?

您已經開發了一個servlet。 對? 它只不過是一個網頁。 如果您需要啟用SSL的網頁,則必須在安裝該證書后購買SSL證書。 如果您沒有該證書,請使用上述代碼。 上述代碼適用於所有安全問題。

您必須從Verisign和Thawte等公司購買SSL / TSL證書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM