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 ...
随机推荐
- ubuntu用户帐号
与用户帐号相关的有几个非常重要的文件/ect/passwd,/etc/shadow,/etc/group /etc/passwd 执行 head -n 5 /ect/passwd显示前5行,内容如下: ...
- docker入门-镜像管理命令篇
一.下载.上传镜像 1:下载安装centos镜像 语法:docker [参数][镜像名称] [root@host1 ~]# docker pull centos Using default tag ...
- 【Java8流】使用学习
[Java8流]使用学习 ============================================= 1.删除子目录及文件 ============================== ...
- Spring、SpringMVC、SpringBoot、SpringCloud的区别与联系
前言 Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架.Spring使你能够编写更干净.更可管理.并且更易于测试的代码. Spring MVC是Spring的一个模块,一个w ...
- Spring Ioc Configration - Annotation
1.配置类注解@Configuration. 2.Bean注解 @Bean. 3.导入其他配置类@Import. 4.回调函数 @Bean(initMethod = "init", ...
- pipelinedb学习笔记 - 1. Continuous Views (连续视图)
Continuous Views 一.Continuous Views 英文直译过来叫连续视图, 在pipelindb中是被定义为专门用来展示 Stream中数据用的.例如:Stream中有一些用户信 ...
- C语言每日一练——第7题
一.题目要求 已知数据文件in.dat中存有200个四位数,把这些数存到数组a中,编写函数jsVal(),其功能是:把千位数字和十位数字重新组成一个新的含有两位数字的数ab(新数的十位数字是原四位数的 ...
- eps-07s,编译及其烧写
项目导入 清理并编译 会出现两个bin文件,然后烧写 修改红框中的东西,然后返回操作界面,进行一键烧写 硬件接线图 设备调试
- Linux 使用vi命令的教程
一.首先用vi命令打卡要编辑的文件: 注意:vi命令的使用如下: 打开或新建文件,并将光标至于第一行首:[root@centos6 /]# vi /etc/my.cnf 打开文件,并将光标移至最后一行 ...
- 搭建私人的云笔记_使用webdav服务
搭建私人的云笔记_使用webdav服务 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-10-10. 手机上有很多云笔记app,大多支持云存储.可是把笔记放在别人的服务器上,总觉得不 ...