java 跳过 SSL

这篇具有很好参考价值的文章主要介绍了java 跳过 SSL。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

java 跳过 SSL

先用postman 测试下,(https://xxxxxxxx.com 地址)是否能访问成功

若在postman 中设置了,关闭SSL 验证

那java 代码里需要做一定来的变更,

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/>
    </parent>
    
    <groupId>org.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

测试代码文章来源地址https://www.toymoban.com/news/detail-520750.html

import com.alibaba.fastjson.JSONObject;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>记录一次post访问,跳过ssl</p>
 *
 * @author YsEz
 * @version V1.0.0
 * @date 2022/5/26 14:54
 */

public class ToKenDataTest {
    protected static Logger log = LoggerFactory.getLogger(ToKenDataTest.class);

    public static void main(String[] args) {
        //uri
        String uri = "https://";
        //头
        Map<String, String> headerMap = new HashMap<>();
        //体
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("app_key", "123");
        paramMap.put("app_value", "456");
        //post 请求 获得token
        String httpPostToken = getHttpPost(uri, headerMap, paramMap);
        //
        System.out.println("---httpPostToken:" + httpPostToken);

    }

    private static String getHttpPost(String uri, Map<String, String> headerMap, Map<String, String> paramMap) {
        HttpClient httpClient = wrapClient();//创建HttpClient,关键点一
        //uri,这里决定用 post,还是用其他方式
        HttpGet httpGet = new HttpGet(uri);
        HttpPost httpPost = new HttpPost(uri);
        HttpDelete httpDelete = new HttpDelete(uri);
        HttpPut httpPut = new HttpPut(uri);
        //头
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            httpPost.addHeader(entry.getKey(), entry.getValue());
        }
        //体
        httpPost.setEntity(new StringEntity(JSONObject.toJSONString(paramMap), ContentType.create("application/json", "utf-8")));
        System.setProperty("jsse.enableSNIExtension", "false");//ssl免检  关键点二
        //访问
        HttpResponse httpResponse;
        String entity = null;
        try {
            httpResponse = httpClient.execute(httpPost);
            entity = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return entity;
    }

    public static HttpClient wrapClient() {//关键点三
        try {
        	SSLContext sc = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        X509Certificate[] x509Certificates, String s) {
                }

                @Override
                public void checkServerTrusted(
                        X509Certificate[] x509Certificates, String s) {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            sc.init(null, new TrustManager[] {tm}, null);
            SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE);
            return HttpClients.custom().setSSLSocketFactory(ssf).build();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            return HttpClients.createDefault();
        }
    }
}

到了这里,关于java 跳过 SSL的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包