第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在不禁用 SNI 的情況下修復(fù) SSLProtocolException:握手警報(bào):

如何在不禁用 SNI 的情況下修復(fù) SSLProtocolException:握手警報(bào):

蕪湖不蕪 2023-02-23 16:14:32
我制作了一個(gè)爬蟲應(yīng)用程序,由于錯(cuò)誤“握手警報(bào):unrecognized_name”,某些網(wǎng)站無法連接。我發(fā)現(xiàn)的大多數(shù)解決方案是通過禁用 SNI 擴(kuò)展 (jsse.enableSNIExtension=false)。但這會(huì)給需要啟用 SNI 的域帶來問題。我怎樣才能只對某些域禁用它?為了進(jìn)行爬行,我使用了 Jsoup,并且因?yàn)槲乙苍谑褂么?,所以我在啟?dòng)時(shí)添加了這段代碼。  private static void disableSslVerification() {    TrustManager[] trustAllCertificates = new TrustManager[] {            new X509TrustManager() {                @Override                public X509Certificate[] getAcceptedIssuers() {                    return null; // Not relevant.                }                @Override                public void checkClientTrusted(X509Certificate[] certs, String authType) {                    // Do nothing. Just allow them all.                }                @Override                public void checkServerTrusted(X509Certificate[] certs, String authType) {                    // Do nothing. Just allow them all.                }            }    };    HostnameVerifier trustAllHostnames = new HostnameVerifier() {        @Override        public boolean verify(String hostname, SSLSession session) {            return true; // Just allow them all.        }    };    try {        System.setProperty("https.protocols", "TLSv1.2,TLSv1.1,SSLv3");       // System.setProperty("jsse.enableSNIExtension", "false");        SSLContext sc = SSLContext.getInstance("SSL");        sc.init(null, trustAllCertificates, new SecureRandom());        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());        HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);    }    catch (GeneralSecurityException e) {        throw new ExceptionInInitializerError(e);    }}如您所見,SNIextension 已被注釋。我會(huì)很感激一個(gè)例子。我要訪問的網(wǎng)址是下一個(gè)。https://www.ocinerioshopping.es/
查看完整描述

1 回答

?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

我設(shè)法通過擴(kuò)展 SSLSocketConnection 并在調(diào)用 createSocket 時(shí)發(fā)送 null 而不是主機(jī)名來解決問題。這樣java就禁用了SNI。然后我只是將新類的一個(gè)實(shí)例傳遞給 Jsoup,我知道 SNI 將在其中失敗。


import javax.net.ssl.*;

import java.io.IOException;

import java.net.InetAddress;

import java.net.Socket;

import java.net.UnknownHostException;

import java.security.KeyManagementException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.cert.X509Certificate;


public class CustomSSLSocketFactory extends SSLSocketFactory {

    private SSLSocketFactory defaultFactory;

    public CustomSSLSocketFactory() throws IOException {

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

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

            }


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

            }


            public X509Certificate[] getAcceptedIssuers() {

                return null;

            }

        }};


        try {

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

            sslContext.init((KeyManager[])null, trustAllCerts, new SecureRandom());

            defaultFactory = sslContext.getSocketFactory();

        } catch (KeyManagementException | NoSuchAlgorithmException var3) {

            throw new IOException("Can't create unsecure trust manager");

        }

    }

    @Override

    public String[] getDefaultCipherSuites() {

       return defaultFactory.getDefaultCipherSuites();

    }


    @Override

    public String[] getSupportedCipherSuites() {

        return defaultFactory.getSupportedCipherSuites();

    }


    @Override

    public Socket createSocket(Socket socket, String s, int i, boolean b) throws IOException {

        //magic happens here, we send null as hostname

        return defaultFactory.createSocket(socket, null, i, b);

    }


    @Override

    public Socket createSocket(String s, int i) throws IOException, UnknownHostException {

        return defaultFactory.createSocket(s,i);

    }


    @Override

    public Socket createSocket(String s, int i, InetAddress inetAddress, int i1) throws IOException, UnknownHostException {

        return defaultFactory.createSocket(s,i,inetAddress,i1);

    }


    @Override

    public Socket createSocket(InetAddress inetAddress, int i) throws IOException {

        return defaultFactory.createSocket(inetAddress, i);

    }


    @Override

    public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress1, int i1) throws IOException {

        return defaultFactory.createSocket(inetAddress,i, inetAddress1, i1);

    }

}

Jsoup 初始化。


Connection conn = Jsoup.connect(url);

conn.sslSocketFactory(new CustomSSLSocketFactory());


查看完整回答
反對 回復(fù) 2023-02-23
  • 1 回答
  • 0 關(guān)注
  • 158 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)