java 主动信任证书
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 主动信任证书的更多相关文章
- java将SSL证书导入系统密钥库
之前安装JIRA和Confluence,配置了SSL证书之后遇到应用程序链接的问题: SSL证书不被信任,导致JIRA和Confluence无法关联. 尝试过很多办法无果之后打算放弃. 最终还是放弃了 ...
- 【Java】Java与数字证书
Java与数字证书 Java与数字证书 证书的签发和应用 证书的内容和意义 其它 证书(Certificate,也称public-key certificate)是用某种签名算法对某些内容(比如公钥) ...
- https本地自签名证书添加到信任证书访问
1.背景 本文适用于基于https(http+ssl)的网站通信.本地调试等,上线是请寻找免费 ssl证书申请. 本地调试过程中,一些特殊的场景需要我使用http+ssl通信,比如在Chrome中使用 ...
- JAVA中SSL证书认证通讯
JAVA中SSL证书认证通讯 SSL通讯服务端 /******************************************************************** * 项目名称 ...
- nginx https ssl 设置受信任证书[转然哥]
nginx https ssl 设置受信任证书[原创] 1. 安装nginx 支持ssl模块 http://nginx.org/en/docs/configure.html yum -y instal ...
- 抓包工具fiddler下载配置(一):下载/安装&信任证书
简介 Fiddler一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,html,js,css等文件 ...
- 【证书】curl 和 java 请求报证书错误
1. 说明: 以下:例子的域名因为工作环境的问题,被我拿自己的博客域名替代了,所以无法进行模拟测试,请珍重,哈哈! 2. 环境: centos:7.5 java jdk:1.8.0_74 3. cur ...
- java HttpClient 忽略证书的信任的实现 MySSLProtocolSocketFactory
当不需要任何证书访问https时,java中先实现一个MySSLProtocolSocketFactory类忽略证书的信任 package com.tgb.mq.producer.utils; imp ...
- httpclient 无信任证书使用https
1.当不需要使用任何证书访问https网页时,只需配置信任任何证书 HttpClient http = new HttpClient(); String url = "https://pay ...
随机推荐
- 一些demo
绑定端口demo: #include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include ...
- MT9V034 全局快门体验总结
MT9V034 全局快门体验总结 部分照片来源网络,尊重版权. . 这个是实物照片 全局快门(相对滚动快门) 拍摄高速物体的效果 高动态效果 低照度和高照度对比 实际拍照效果图(来自网友华健) 特殊应 ...
- margin重叠与穿透问题
margin重叠是指两个同级元素之间.margin穿透指的是,子元素margin超出父元素而未被父元素包含的现象. 出现margin重叠的原因: 同一个BFC里面两个块级元素会出现margin折叠. ...
- 551. 学生出勤纪录 I
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤纪录中不超过一个' ...
- C# 导出pdf(浏览器不预览直接下载)
一.接口部分的代码 [HttpGet] public HttpResponseMessage ExportPdf(string id) { string pdfName = ""; ...
- 带你使用Visual Studio 2019创建一个MVC Web应用
工欲善其事必先利其器,我们既然有Visual Studio2019这样的IDE为什么不用?学.Net Core而不用Visual Studio进行开发可谓是多么另类呀!既然你已经安装了VS2019的话 ...
- docker安装与配置
Docker与虚拟化技术的区别 虚拟机分配多少宿主机就减少多少资源,比如VMware1分配了2Gb内存,如果运行5Gb的应用程序会造成内存溢出,vmware2分配了2Gb内存,如果运行2Gb的应用程序 ...
- Linux系统基础知识
文件类型属性 '-'代表普通文件 'd'代表目录文件 'l'代表链接文件link 'b'代表块文件block 'c'代表字符设备文件 'p'代表管道文件
- 重学前端--js是面向对象还是基于对象?
重学前端-面向对象 跟着winter老师一起,重新认识前端的知识框架 js面向对象或基于对象编程 以前感觉这两个在本质上没有什么区别,面向对象和基于对象都是对一个抽象的对象拥有一系列的行为和状态,本质 ...
- 《Java基础知识》Java锁详解(volatile,synchronized等)
volatile: 让变量每次在使用的时候,都从主存中取. volatile具有synchronized关键字的“可见性”,但是没有synchronized关键字的“并发正确性”,也就是说不保证线程执 ...