java连接https时禁用证书验证.
import java.io.File;
import java.security.cert.CertificateException;
import java.util.List;
import java.util.Map; import javax.net.ssl.SSLContext; import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate; public String test_getssoinfo() throws Exception { TrustStrategy acceptingTrustStrategy = new TrustStrategy() { @Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
System.out.println("in isTrusted" );
// TODO 完成方法实现
return true;
}
} ; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build(); HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); RestTemplate restTemplate = new RestTemplate(requestFactory);
// RestTemplate restTemplate = new RestTemplate();
String userJsonStr = restTemplate.getForObject("https://www.xxx.yy/", String.class);
return userJsonStr;
}
okhttp:
X509TrustManager trustManager = new X509TrustManager()
{
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException
{
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException
{
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
StrUtil.log("getAcceptedIssuers()");
return new X509Certificate[0];
}
}; SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new X509TrustManager[] { trustManager }, new java.security.SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); builder.sslSocketFactory(sslSocketFactory, trustManager).hostnameVerifier(new HostnameVerifier()
{
@Override
public boolean verify(String hostname, SSLSession session)
{
StrUtil.log("verify() hostname:" + hostname + ", session:" + (session != null ? session.toString() : "null"));
return true;
}
});
java连接https时禁用证书验证.的更多相关文章
- How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查)
How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查) **** ...
- Java访问HTTPS时证书验证问题
为了尽可能避免安全问题,公司的很多系统服务都逐步https化,虽然开始过程会遇到各种问题,但趋势不改.最完美的https应用是能实现双向认证,客户端用私钥签名用服务端公钥加密,服务端用私钥签名客户端都 ...
- Axis 1 https(SSL) client 证书验证错误ValidatorException workaround
Axis 1.x 编写的client在测试https的webservice的时候, 由于client 代码建立SSL连接的时候没有对truststore进行设置,在与https部署的webservic ...
- HTTPS请求 SSL证书验证
import urllib2 url = "https://www.12306.cn/mormhweb/" headers = {"User-Agent": & ...
- java连接hbase时出现....is accessible from more than one module:
今天在用java程序连接hbase时,出现错误,The package org.apache.hadoop.hbase is accessible from more than one module: ...
- 三步解决fiddler升级后https无法通过证书验证问题
有时候使用fiddler时,https页面会出现错误提示,我们可以这样设置来避免错误 第一步:去掉https的抓取 Tools>Option 去掉Capture HTTPS CONNECTs 的 ...
- 记一次 java 连接 linux ssh服务 权限验证失败的原因和解决过程
下面的问题我是通过之前的ssh测试类找出原因的,因为我的测试类跑通了,但是程序跑不通,看了一下源码发现还有一处没有进行解密,所以才会权限验证失败. // 出现权限验证失败的原因就在这里,因为老板要求对 ...
- java连接sql server--关于登录验证及对数据库增删改查应用
一:步骤## 1.sql server建立数据库和相关表 2.建立数据源 (1).打开控制面板找到管理,打开ODBC选项或直接搜索数据源 (2).打开数据源配置后点击添加,选择sql server ...
- 调用微信退款接口时,证书验证出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置- ...
随机推荐
- 理解metrics.classification_report
混淆矩阵是一个矩阵,类别个数可以有多个,a[i][j]表示将类别i的样本误判为类别j的个数. classification_report用来分析不同类别的准确率,召回率,F1值等,从而便于按照类别查看 ...
- Windows phone 应用开发系列教程(更新中)
Windows phone 应用开发[1]-Text To Speech 作为开篇章节.第一篇将在如下介绍一些Windows phone比较有意思的东西-Text To Speech[文 ...
- JavaScript Math Object 数字
JavaScript Math Object Math Object The Math object allows you to perform mathematical tasks. Math is ...
- dataset string dataset
DataSet ds = new DataSet(); ds.ReadXml(dataFile); Console.WriteLine(data.Length); string dataxml = d ...
- Android Studio找不到FragmentActivity类
右击项目——>open module settings——>选择第五个选项卡“Dependencies”——>点击加号——>选择第一个Library dependency——& ...
- WARNING: The host 'r6' could not be looked up with /usr/local/mysql/bin/resolveip.
初始化MySQL数据库提示以下信息: # /usr/local/mysql/scripts/mysql_install_db \ > --defaults-file=/etc/my.cnf \ ...
- Android 自定义可拖拽View,界面渲染刷新后不会自动回到起始位置
以自定义ImageView为例: /** * 可拖拽ImageView * Created by admin on 2017/2/21. */ public class FloatingImageVi ...
- hihoCoder 1015 KMP算法(kmp)
#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣.他们约定好互相帮助 ...
- SharePoint 2013 Step by Step—— How to Upload Multiple Documents in Document Library
How to Upload Multiple documents in SharePoint 2013,Options to add multiple files in a document libr ...
- Knockout: 让ViewModel从htm中剥离出去。
在一些Knockout例子中,直接在htm中添加scripts写viewmodel,如何能将让ViewModel从htm中剥离出去呢?从knockout官网上找到了解决方法,如下: 1.knockou ...