Java项目需要由于客户端发起的https请求,支持https请求
package com.xzz.FileText;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import javax.net.ssl.SSLContext;
/**
* @ClassName SSL
* @Date 20210724
* @Author lcl-xiezezhong
* @Version 1.0
*/
@Slf4j
public class SSLContextTest {
public static void main(String[] args) {
String url = "https:money.ch.com/m/psbc";
HttpClientBuilder custom = HttpClients.custom();
if (url.startsWith("https")) {
// 信任所有
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
(chain, authType) -> true).build();
//不对主机名进行校验,验证功能被关闭,空操作
SSLConnectionSocketFactory ssl = new SSLConnectionSocketFactory(sslContext, (s, sslSession) -> true);
custom.setSSLSocketFactory(ssl);
log.info("不校验https");
} catch (Exception e) {
log.info("HTTPS创建异常,", e);
}
}
}
}