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

湘潭网站建设 磐石网络在哪深圳网站建设补助

湘潭网站建设 磐石网络在哪,深圳网站建设补助,炫丽的网站,SEO参与网站建设注意本文将展示如何配置Apache HttpClient 4和5以支持“接受所有”SSL。 目标很简单——访问没有有效证书的HTTPS URL。 SSLPeerUnverifiedException 在未配置SSL的情况下,尝试消费一个HTTPS URL时会遇到以下测试失败: 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 Registry<ConnectionSocketFactory> 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);Registry<ConnectionSocketFactory> 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);ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);assertThat(response.getStatusCode().value(), equalTo(200));
}

Spring RestTemplate与SSL(HttpClient 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 ResponseEntity<String> 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 Registry<ConnectionSocketFactory> 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 ResponseEntity<String> 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.yayakq.cn/news/830976/

相关文章:

  • 网站开发kpi专业上海网站建设
  • wordpress 后台演示seo网站关键词优化快速官网
  • 智慧团建网站官网入口登录厦门数字引擎 怎么打不开网站
  • 网站开发技能有哪些高端网站建设的市场
  • dream网站怎么做框架电商线上推广
  • 云集网站哪个公司做的酒店类网站建设开发书
  • 公司网站是不是每天要更新北京网站建立公司
  • 深圳网站备案拍照点网络广告推广计划方案
  • 网站域名个人备案跟企业备案北京南站在几环
  • 如何在八戒网便宜做网站无锡 网站 seo 优化
  • 汨罗哪里有网站开发的公司电话海外网络连接
  • 现在做网站还用dw做模板了吗wordpress浏览次数插件
  • 南昌商城网站建设wordpress怎么解决后台编辑器卡顿
  • 需要品牌网站建设长沙好的网站建设公司哪家好
  • 湛江免费企业建站企业网络推广的简介
  • 资讯网站怎么做广告联盟接广告
  • 通栏式网站aaaa景区网站建设标准
  • 网站服务器管理系统建设银行永泰支行网站
  • 长沙开发网站的公司哪家好网站建设行业发展史
  • 佛山网站建设公司分享微信营销的五个技巧重庆大江建设工程集团有限公司
  • access做调查表网站怎样买网站建设
  • 足球世界排名国家2023网站seo
  • 建站网站关键词大全网页开发人员工具
  • 制作网站吗自己做网站发信息
  • 锦州网站建设公司四川建设银行官网招聘网站
  • 科技无国界wordpress sql优化
  • 太原做网站直通车网上书店网站前端搜索条怎么做
  • 好网站具备条件汕头建设银行协华支行
  • 潍坊网站开发招聘信息wordpress 导航站 模板
  • 咸阳学校网站建设费用网站怎么换主机