当前位置: 首页 > news >正文

建设网站需要冠县做网站

建设网站需要,冠县做网站,logo图片生成器在线制作,网络优化首先要有网站本文将展示如何配置Apache HttpClient 4和5以支持“接受所有”SSL。 目标很简单——访问没有有效证书的HTTPS URL。 SSLPeerUnverifiedException 在未配置SSL的情况下#xff0c;尝试消费一个HTTPS URL时会遇到以下测试失败#xff1a; Test void whenHttpsUrlIsConsumed…本文将展示如何配置Apache HttpClient 4和5以支持“接受所有”SSL。 目标很简单——访问没有有效证书的HTTPS URL。 SSLPeerUnverifiedException 在未配置SSL的情况下尝试消费一个HTTPS URL时会遇到以下测试失败 Test void whenHttpsUrlIsConsumed_thenException() {String urlOverHttps https://localhost:8082/httpclient-simple;HttpGet getMethod new HttpGet(urlOverHttps);assertThrows(SSLPeerUnverifiedException.class, () - {CloseableHttpClient httpClient HttpClients.createDefault();HttpResponse response httpClient.execute(getMethod, new CustomHttpClientResponseHandler());assertThat(response.getCode(), equalTo(200));}); }具体的失败信息是 javax.net.ssl.SSLPeerUnverifiedException: peer not authenticatedat sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:397)at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:126)...当无法为URL建立有效的信任链时就会抛出javax.net.ssl.SSLPeerUnverifiedException异常。 配置SSL - 接受所有HttpClient 5 现在让我们配置HTTP客户端以信任所有证书链无论其有效性如何 Test void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException, IOException {final HttpGet getMethod new HttpGet(HOST_WITH_SSL);final TrustStrategy acceptingTrustStrategy (cert, authType) - true;final SSLContext sslContext SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();final SSLConnectionSocketFactory sslsf new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);final RegistryConnectionSocketFactory socketFactoryRegistry RegistryBuilder.ConnectionSocketFactory create().register(https, sslsf).register(http, new PlainConnectionSocketFactory()).build();final BasicHttpClientConnectionManager connectionManager new BasicHttpClientConnectionManager(socketFactoryRegistry);try (CloseableHttpClient httpClient HttpClients.custom().setConnectionManager(connectionManager).build();CloseableHttpResponse response (CloseableHttpResponse) httpClient.execute(getMethod, new CustomHttpClientResponseHandler())) {final int statusCode response.getCode();assertThat(statusCode, equalTo(HttpStatus.SC_OK));} }通过新的TrustStrategy覆盖标准证书验证过程后测试现在可以通过客户端能够成功消费HTTPS URL。 配置SSL - 接受所有HttpClient 4.5 对于HttpClient 4.5版本配置方式类似但使用了一些不同的API Test public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk()throws GeneralSecurityException {TrustStrategy acceptingTrustStrategy (cert, authType) - true;SSLContext sslContext SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();SSLConnectionSocketFactory sslsf new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);RegistryConnectionSocketFactory socketFactoryRegistry RegistryBuilder.ConnectionSocketFactory create().register(https, sslsf).register(http, new PlainConnectionSocketFactory()).build();BasicHttpClientConnectionManager connectionManager new BasicHttpClientConnectionManager(socketFactoryRegistry);CloseableHttpClient httpClient HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(connectionManager).build();HttpComponentsClientHttpRequestFactory requestFactory new HttpComponentsClientHttpRequestFactory(httpClient);ResponseEntityString response new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);assertThat(response.getStatusCode().value(), equalTo(200)); }Spring RestTemplate与SSLHttpClient 5 了解了如何配置带有SSL支持的基本HttpClient之后我们来看看更高级别的客户端——Spring RestTemplate。 在没有配置SSL的情况下预期的测试会失败 Test void whenHttpsUrlIsConsumed_thenException() {final String urlOverHttps https://localhost:8443/httpclient-simple/api/bars/1;assertThrows(ResourceAccessException.class, () - {final ResponseEntityString response new RestTemplate().exchange(urlOverHttps, HttpMethod.GET, null, String.class);assertThat(response.getStatusCode().value(), equalTo(200));}); }接下来配置SSL来解决这个问题 Test void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException {final TrustStrategy acceptingTrustStrategy (cert, authType) - true;final SSLContext sslContext SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();final SSLConnectionSocketFactory sslsf new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);final RegistryConnectionSocketFactory socketFactoryRegistry RegistryBuilder.ConnectionSocketFactory create().register(https, sslsf).register(http, new PlainConnectionSocketFactory()).build();final BasicHttpClientConnectionManager connectionManager new BasicHttpClientConnectionManager(socketFactoryRegistry);final CloseableHttpClient httpClient HttpClients.custom().setConnectionManager(connectionManager).build();final HttpComponentsClientHttpRequestFactory requestFactory new HttpComponentsClientHttpRequestFactory(httpClient);final ResponseEntityString response new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);assertThat(response.getStatusCode().value(), equalTo(200)); }这里配置方式与直接使用HttpClient非常相似我们用带有SSL支持的请求工厂配置了RestTemplate。 结论 本教程讨论了如何配置Apache HttpClient以使其能够消费任何HTTPS URL无论证书的有效性如何。 同样也展示了如何对Spring RestTemplate进行同样的配置。 重要的是要理解这种策略完全忽略了证书检查——这使得它不安全仅应在合理的情况下使用。
http://www.w-s-a.com/news/406126/

相关文章:

  • 保定网站seo哪家公司好wordpress教程视频下载
  • 网站开发 哪些文档网站海外推广方法
  • 广西建设局网站首页如何做条形码网站怎么搞
  • 琼海建设网站wordpress 商城站下载地址
  • 网站需要多大数据库divider wordpress
  • 兰州北京网站建设网络广告推广网站
  • 宁晋网站建设森网站建设
  • 网站没有收录原因trel域名
  • 建设门户网站的目的和需求台州专业网站建设方案
  • 苏州网站建设系统方案成都行业网站设计
  • wordpress多说读者墙seo分析师招聘
  • 视频网站开发计划书wordpress文件详情
  • 重庆付费网站推广电商网站 开发周期
  • thinkcmf 做企业网站视频播放类网站建设费用
  • vps网站助手大学选修课网站建设
  • 南浦电商网站建设北京海淀社保网站
  • 传奇网站模板怎么做的吗大连警方最新通告
  • 成都私人做公司网站的北京网站建设需要多少钱
  • 魔客吧是什麼程序做的网站代理厦门网站设计公司
  • 90设计手机站东营网站推广
  • 哪家购物网站建设好专门做水生植物销售网站
  • php医院网站开发兼职app开发网上app开发
  • 接任务做兼职的的网站衡阳手机网站设计
  • 徐州经济开发区网站佛山百度关键词seo外包
  • 肃宁网站建设有限责任公司法人承担什么责任
  • 珠海斗门建设局网站如何免费做网站
  • 自助外贸网站建设可直接打开网站的网页
  • 江苏城嘉建设工程有限公司网站潍坊网站定制公司
  • 四川省住房和城乡建设厅新网站宜昌建设厅网站
  • 建设网站一般流程建设开发网站