java 主动信任证书

SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mid.toCharArray()).loadTrustMaterial(keyStore, new TrustStrategy() {
// 信任所有
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) {
return true;
}
}).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

包: https://files.cnblogs.com/files/xiluhua/httpclient-4.3.3.zip

完整代码:

package com.taiping.dianshang.payment.service.wechat.fromWechat;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.stereotype.Component; import com.taiping.dianshang.constant.ConstantTool;
import com.taiping.facility.cache.container.CacheContainer;
import com.taiping.facility.tool.FileStreamTool;
import com.taiping.facility.tool.LogTool;
import com.taiping.facility.tool.MapTool;
import com.taiping.facility.tool.PropertyFileTool; /**
*
* @author xilh
* @since 20200409
*/
@Component
public class HttpclientImpl_ssl { /**
* @author xilh
* @since 20200324
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public String post(String url, Object obj, Map<String, Object> httpclientParams) throws Exception {
HttpPost httpPost = null;
String responseMsg = null;
Map<String, Object> map = (Map<String, Object>)obj;
String trans = MapTool.getStringFromMap(map, "trans");
String packet = MapTool.getStringFromMap(map, "packet"); DefaultHttpClient httpclient = new DefaultHttpClient();
// 代理的设置
String value = CacheContainer.getSystemParameterValue("internet.proxy");
if (LogTool.isLocal) {
value = PropertyFileTool.get("internet.proxy");
}
LogTool.info(this.getClass(), trans+", proxy: "+value);
String[] arr = value.split(":");
HttpHost proxy = new HttpHost(arr[0], Integer.valueOf(arr[1]));
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); try {
// Secure Protocol implementation.
SSLContext ctx = SSLContext.getInstance("TLS");
// Implementation of a trust manager for X509 certificates
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
} public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
} public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = httpclient.getConnectionManager();
// register https protocol in httpclient's scheme registry
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
httpclient = new DefaultHttpClient(ccm, httpclient.getParams());
// String auth = CacheContainer.getSysParamValue("authorization.token.18", true);
// appKey :"TPJJ" appSecret: "yMJlPH9RnjxpqhyysxaIXYT82U1Sh32q"
//装配post请求参数
httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type","application/json;charset=UTF-8");
// List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
// list.add(new BasicNameValuePair("appKey", "TPJJ"));
// list.add(new BasicNameValuePair("appSecret", "yMJlPH9RnjxpqhyysxaIXYT82U1Sh32q"));
// list.add(new BasicNameValuePair("tokenExpireDay", "7"));
httpPost.setEntity(new StringEntity((String)packet,"application/json", ConstantTool.UTF8));
HttpResponse response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity();
byte buffer[] = FileStreamTool.read(entity.getContent());
LogTool.info(this.getClass(), trans+", buffer size: "+buffer.length);
if (buffer != null && buffer.length > 0) {
responseMsg = new String(buffer, ConstantTool.UTF8);
}
LogTool.info(this.getClass(), trans+", responseMsg: "+responseMsg);
System.out.println();
} catch (Exception e) {
LogTool.error(this.getClass(), e);
} finally{
if (httpPost != null) {
// 关闭请求
httpPost.releaseConnection();
}
} return responseMsg;
} }

  

java 主动信任证书的更多相关文章

  1. java将SSL证书导入系统密钥库

    之前安装JIRA和Confluence,配置了SSL证书之后遇到应用程序链接的问题: SSL证书不被信任,导致JIRA和Confluence无法关联. 尝试过很多办法无果之后打算放弃. 最终还是放弃了 ...

  2. 【Java】Java与数字证书

    Java与数字证书 Java与数字证书 证书的签发和应用 证书的内容和意义 其它 证书(Certificate,也称public-key certificate)是用某种签名算法对某些内容(比如公钥) ...

  3. https本地自签名证书添加到信任证书访问

    1.背景 本文适用于基于https(http+ssl)的网站通信.本地调试等,上线是请寻找免费 ssl证书申请. 本地调试过程中,一些特殊的场景需要我使用http+ssl通信,比如在Chrome中使用 ...

  4. JAVA中SSL证书认证通讯

    JAVA中SSL证书认证通讯 SSL通讯服务端 /******************************************************************** * 项目名称 ...

  5. nginx https ssl 设置受信任证书[转然哥]

    nginx https ssl 设置受信任证书[原创] 1. 安装nginx 支持ssl模块 http://nginx.org/en/docs/configure.html yum -y instal ...

  6. 抓包工具fiddler下载配置(一):下载/安装&信任证书

    简介 Fiddler一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,html,js,css等文件 ...

  7. 【证书】curl 和 java 请求报证书错误

    1. 说明: 以下:例子的域名因为工作环境的问题,被我拿自己的博客域名替代了,所以无法进行模拟测试,请珍重,哈哈! 2. 环境: centos:7.5 java jdk:1.8.0_74 3. cur ...

  8. java HttpClient 忽略证书的信任的实现 MySSLProtocolSocketFactory

    当不需要任何证书访问https时,java中先实现一个MySSLProtocolSocketFactory类忽略证书的信任 package com.tgb.mq.producer.utils; imp ...

  9. httpclient 无信任证书使用https

    1.当不需要使用任何证书访问https网页时,只需配置信任任何证书 HttpClient http = new HttpClient(); String url = "https://pay ...

随机推荐

  1. git 中的 merge 和 rebase

    示例分支:master . dev 把 dev 分支上的新内容合并到 master 上 先切换分支到master git checkout master 合并操作 git merge dev 或者 g ...

  2. python爬虫--selenium模块.上来自己动!

    selenium 基本操作 from selenium import webdriver from time import sleep #实例化一个浏览器对象 bro = webdriver.Chro ...

  3. 计算机等级考试真题2(JAVA)

    答案: 解析: (注:解析部分是博主用所学知识以及在百度上搜索总结出来的)   1. D (A)类属于JAVA语言的引用数据类型. (B)接口属于JAVA语言的引用数据类型. (C)数组属于JAVA语 ...

  4. 用函数式编程,从0开发3D引擎和编辑器(二):函数式编程准备

    大家好,本文介绍了本系列涉及到的函数式编程的主要知识点,为正式开发做好了准备. 函数式编程的优点 1.粒度小 相比面向对象编程以类为单位,函数式编程以函数为单位,粒度更小. 正所谓: 我只想要一个香蕉 ...

  5. 墨者 - X-FORWARDED-FOR注入漏洞实战

    X-FORWARDED-FOR 首先,X-Forwarded-For 是一个 HTTP 扩展头部.HTTP/1.1(RFC 2616)协议并没有对它的定义,它最开始是由 Squid 这个缓存代理软件引 ...

  6. react-native run-ios “Could not find iPhone X simulator”

    问题 这个问题发生在旧的RN版本(0.57,0.58(<0.58.4),-)和Xcode 10.3中,其中可用模拟器的名称得到了一些调整 在文件node_modules/@react nativ ...

  7. 通过ADB获取Android手机信息

    原文:https://blog.csdn.net/fasfaf454/article/details/51438743 1.获取手机系统信息( CPU,厂商名称等)adb shell "ca ...

  8. Linux下修改MySQL数据库数据文件路径

    使用rpm安装方式安装完MySQL数据库后,数据文件的默认路径为/var/lib/mysql,然而根目录并不适合用于存储数据文件. 原路径:/var/lib/mysql 目标路径:/home/mysq ...

  9. 求连通块个数 - BFS、DFS、并查集实现

    本文基于leetcode的200.岛屿数量(题目

  10. xml-mapping xml 与 java 对象转换映射框架,像 XStream 一样优雅地读写xml

    xml xml 是 java 实现的 xml 框架. 希望以最优雅的方式进行 xml 和 java 之间的转换处理,一行代码搞定一切. 特点 对象的和 xml 的互相映射 支持注解 @Alias 指定 ...