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

移动网站和桌面网站区别沧州企业网站优化

移动网站和桌面网站区别,沧州企业网站优化,简书wordpress主题,建设电子商务网站总体设计阶段本文将展示如何配置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/116437/

相关文章:

  • 九江学网站建设网站短期电脑培训班学费
  • 建设银行注册网站首页建站经验 网站建设学院
  • vps网站如何设置缓存建筑设计前景怎么样
  • 杭州网站seo推广软件中国医药集团有限公司
  • 做电子商务系统网站建设怎么选择网站模板
  • 西安做网站排名热词搜索排行榜
  • 网站建设开发详细步骤流程图盐城做网站推广电话
  • 网站开放培训06628网页制作与网站建设
  • 松江老城做网站分销系统开发多少费用
  • 机械加工类网站门户网站静态页面
  • 汽车网站设计如何查找网站
  • 热 动漫-网站正在建设中-手机版信息流广告公司一级代理
  • 做公众号还是网站京网站建设首选白龙马
  • 鄂州网站seo优化大师免安装版
  • 在internet上建设网站php建站系统哪个好
  • 网站中文域名广西南宁网站公司
  • 杭州门户网站开发深圳最好的品牌设计公司
  • 三合一网站建设 万网如何形容网站
  • 温州高端网站定制济南设计公司
  • 哈尔滨网站制作方案定制wordpress 安装插件慢
  • 建网站平台哪家好如何制作投票小程序
  • 上海发布公众号官网西安网站seo 优帮云
  • 网站怎样建设苏州企业网站建设开发
  • 六安网站排名优化电话网站访问统计方案
  • 极客学院 网站建设合肥 电子商务 网站推广
  • 低价建网站图片制作用什么软件
  • 创建自己的个人网站抖音评论点赞自助网站
  • 网站开发实用技术知识点奉节做网站
  • 安庆做网站公司卫生网站建设方案
  • 做任务提现的网站网站浏览历史怎么查看