重写ssl对网关站点证书进行证书校验 绕过https安全证书校验

因为场景是调用对方的WebService方法,所以 发送的方法体是自定义xml格式的,api请求根据自己实际业务情况改以下就好了。

package cn.relabo.tszc.bench.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * @author zjh
 */
public class HttpClentUtils {

    /**
     * 重写ssl对网关站点证书进行证书校验
     * @param url
     * @param body
     * @param contentType
     * @return
     * @throws Exception
     */
    public static String sendSSL(String url, String body, String contentType) throws Exception {
        // 创建SSLClient连接客户端
        CloseableHttpClient client = createSSLClient();
        // 创建url的post请求对象
        HttpPost post = new HttpPost(url);
        HttpEntity entity = new StringEntity(body, "utf-8");
        // 将请求信息装载到post的entity中
        post.setEntity(entity);
        if (contentType == null || "".equals(contentType)) {
            contentType = "text/html";
        }
        post.setHeader("Content-Type", contentType);
        // 请求url获取响应
        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() == 200) {
            String resEntityStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            client.close();
            resEntityStr = getReturn(resEntityStr);
            return resEntityStr;
        } else if (response.getStatusLine().getStatusCode() == 404) {
            client.close();
            throw new Exception("Exception has occurred.");
        } else {
             client.close();
            throw new Exception();
        }
    }

    public static CloseableHttpClient createSSLClient() {
        SSLContext sslContext = null;
        try {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    // 通过所有证书
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    // 不验证hostname
                    return true;
                }
            });
            // 如果异常了,创建普通的client
            return HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();
    }

    /**
     * 接收参数,返回wsdl报文格式
     */
    public static String getBody(String ser,String params,String xmlns){
        return "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\""+xmlns+"\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <ser:"+ser+">\n" +
                "         <!--Optional:-->\n" +
                params +
                "      </ser:"+ser+">\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
    }

    public static String getReturn(String body){
        String json = "";
        String[] split1 = body.split("<return>");
        if(split1.length > 1){
            String[] split = split1[1].split("</return>");
            json= split[0];
        }
        return json;
    }
    public static String getParams(String params, String paramsName) {
        return "<"+paramsName+">" + params + "</"+paramsName+">";
    }
    public static void main(String[] args) {
        try {
            System.out.println("test2...");
            String url = "http://localhost:8088/sap/mobileService?wsdl";
            String body = HttpClentUtils.getBody("baseInfo_benchMng_getBench",
                    "<benchid>1</benchid>\n","http://service.server.mobile.webService.project.tszc.relabo.cn");
            String contentType = "application/xml;charset=utf-8";
            String info = HttpClentUtils.sendSSL(url, body, contentType);
            System.out.println("setObjectKey.result=" + info);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("shibai");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值