亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法在柑橘框架中關閉 ssl 驗證?

有沒有辦法在柑橘框架中關閉 ssl 驗證?

MM們 2023-05-10 13:37:01
我想測試 https (api) 客戶端。但我想關掉ssl validation柑橘。你能告訴我如何關閉 ssl 驗證嗎?(httpActionBuilder -> httpActionBuilder  .client("https://localhost") .send() .get() .header(....)這是自動檢查ssl證書。我收到以下異常:PKIX 路徑構建失?。簊un.security.provider.certpath.SunCertPathBuilderException:無法找到請求目標的有效證書路徑;嵌套異常是 javax.net.ssl.SSLHandshakeException。有沒有辦法關閉 ssl 驗證(就像我們ssl在 postman 或任何其他框架中禁用驗證一樣)。我將如何將上下文添加到柑橘測試的 ssl 上下文http(httpActionBuilder -> httpActionBuilder .client(" https://....com ") .send() .get() .header(....)
查看完整描述

2 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

添加以下類


class NonValidatingTrustManager implements X509TrustManager {


    @Override

    public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

    }


    @Override

    public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

    }


    @Override

    public X509Certificate[] getAcceptedIssuers() {

        return new X509Certificate[0];

    }

}

然后在 Spring 中@Configuration通過聲明新 bean 覆蓋默認的 SSLContext


@Bean

public SSLContext sslContext() throws KeyManagementException, NoSuchAlgorithmException {

    SSLContext ctx = SSLContext.getInstance("TLS"); // or try "SSL"

    ctx.init(null, new TrustManager[] { new NonValidatingTrustManager() }, null);

    return ctx;

}

更新


對于測試,您創建單獨的配置類并將覆蓋的 SSLContext 也放在那里:


@TestConfiguration

class TestConfig {


    @Bean

    public SSLContext sslContext() throws Exception {

        SSLContext ctx = SSLContext.getInstance("TLS"); // or try "SSL"

        ctx.init(null, new TrustManager[] { new NonValidatingTrustManager() }, null);

        return ctx;

    }

}

并在單元測試中使用它,例如:


@SpringBootTest(classes = { TestConfig.class })

class MyTest {

    ...

}


查看完整回答
反對 回復 2023-05-10
?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

您可以嘗試以下代碼。我正在嘗試允許所有受信任的證書,以便您可以進行 https 調用。


try {

TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }

            public void checkClientTrusted(X509Certificate[] certs, String authType) { }

            public void checkServerTrusted(X509Certificate[] certs, String authType) { }


        } };


SSLContext sc = SSLContext.getInstance("SSL");

sc.init(null, trustAllCerts, new java.security.SecureRandom());

SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(

                sslcontext, NoopHostnameVerifier.INSTANCE);     

HttpClients.custom()

                .setSSLSocketFactory(sslSocketFactory)

                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)

                .build();               

}

catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {

        throw new BeanCreationException("Failed to create http client for ssl connection", e);

}


查看完整回答
反對 回復 2023-05-10
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號