hutool发送https请求,配置extensions的SNI

文章介绍了如何在Java中使用Hutool库的HttpRequest和ExampleSSLFactory类,定制SSL连接以支持特定的协议和设置SNIHostName,以实现对https://www.example.com的安全访问。
摘要由CSDN通过智能技术生成

废话不多说直接上代码 

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;


public class Application {

    public static void main(String[] args){

        final HttpRequest get = HttpUtil.createGet("https://www.example.com");
        //new的是下面的类
        get.setSSLSocketFactory(new ExampleSSLFactory());
        final HttpResponse executeRes = get.execute();

        String res = executeRes.body();
        System.out.println(res);


    }
}
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.net.SSLUtil;
import cn.hutool.core.util.ArrayUtil;

import javax.net.ssl.SNIHostName;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Collections;

/**
 * 其实就是copy了,cn.hutool.http.ssl.CustomProtocolsSSLFactory
 */
public class ExampleSSLFactory extends SSLSocketFactory {

    private final String[] protocols;
    private final SSLSocketFactory base;

    public MeSSLFactory(String... protocols) throws IORuntimeException {
        this.protocols = protocols;
        this.base = SSLUtil.createSSLContext((String)null).getSocketFactory();
    }


    public String[] getDefaultCipherSuites() {
        return this.base.getDefaultCipherSuites();
    }

    public String[] getSupportedCipherSuites() {
        return this.base.getSupportedCipherSuites();
    }

    public Socket createSocket() throws IOException {
        SSLSocket sslSocket = (SSLSocket)this.base.createSocket();
        this.resetProtocols(sslSocket);
        this.setSNIHostName(sslSocket);
        return sslSocket;
    }

    public SSLSocket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
        SSLSocket socket = (SSLSocket)this.base.createSocket(s, host, port, autoClose);
        this.resetProtocols(socket);
        this.setSNIHostName(socket);
        return socket;
    }

    public Socket createSocket(String host, int port) throws IOException {
        SSLSocket socket = (SSLSocket)this.base.createSocket(host, port);
        this.resetProtocols(socket);
        this.setSNIHostName(socket);
        return socket;
    }

    public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
        SSLSocket socket = (SSLSocket)this.base.createSocket(host, port, localHost, localPort);
        this.resetProtocols(socket);
        this.setSNIHostName(socket);
        return socket;
    }

    public Socket createSocket(InetAddress host, int port) throws IOException {
        SSLSocket socket = (SSLSocket)this.base.createSocket(host, port);
        this.resetProtocols(socket);
        this.setSNIHostName(socket);
        return socket;
    }

    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
        SSLSocket socket = (SSLSocket)this.base.createSocket(address, port, localAddress, localPort);
        this.resetProtocols(socket);
        this.setSNIHostName(socket);
        return socket;
    }

    private void resetProtocols(SSLSocket socket) {
        if (ArrayUtil.isNotEmpty(this.protocols)) {
            socket.setEnabledProtocols(this.protocols);
        }

    }

    private void setSNIHostName(SSLSocket socket){
        // 创建一个SSLParameters对象
        SSLParameters sslParameters = new SSLParameters();

        // 添加一个SNIHostName实例到SSLParameters对象中
        sslParameters.setServerNames(Collections.singletonList(new SNIHostName("www.example.com")));

        socket.setSSLParameters(sslParameters);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值