spring restemplate:403异常(有时)

ecbunoof  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(537)

我有一个对api(get)的restemplate调用。这个调用,是我们唯一的get类型,并通过代理。似乎有时在一周内,调用会返回一个403禁止的异常:“sun.security.validator.validatorexception”
我们在spring和api之间有一个证书,但是这个证书工作得很好(应用程序在一天内返回数千个“200ok”)。
但有时,只有这个调用(而不是其他post)返回“403 forbidden”。
我们已经做到了:
通过代理使用curl启动jmeter(一切似乎都正常)
禁用信任库仅用于测试(结果为ko)
这是resttemplate代码:

  1. SSLConnectionSocketFactory socketFactory;
  2. socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder()
  3. .loadTrustMaterial(ResourceUtils.getFile(this.trustStorePath), this.trustStorePassword.toCharArray())
  4. .loadKeyMaterial(ResourceUtils.getFile(this.keyStorePath), this.keystorePassword.toCharArray(),
  5. this.keystorePassword.toCharArray())
  6. .build(), NoopHostnameVerifier.INSTANCE);
  7. CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(socketFactory).setProxy(host)
  8. .disableCookieManagement().disableRedirectHandling().build();
  9. ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
  10. RestTemplate restTemplateVar = new RestTemplate(requestFactory);

这就是电话:

  1. response = this.restTemplate.getForEntity(this.host, String.class);

并发连接的数量可能是原因吗?
为什么只有得到和有时?
最后一个:如果我们通过httpconnection更改restemplate,结果可能会有所不同?
提前谢谢

rkttyhzu

rkttyhzu1#

设置此属性可以很好地工作(这取决于您的度量)
.setMaxConntTotal(1000)
.setmaxconnperroute(40)

  1. CloseableHttpClient client = HttpClients.custom()
  2. .setSSLSocketFactory(socketFactory)
  3. .setProxy(host)
  4. .disableCookieManagement()
  5. .disableRedirectHandling()
  6. .setMaxConnTotal(1000)
  7. .setMaxConnPerRoute(40)
  8. .build();

相关问题