Spring RestTemplate: 403 Exception (sometimes)



我有一个对API (get)的RestTemplate调用。这个调用,是我们仅有的GET类型的调用,并且要经过代理。似乎有时在一周内,调用返回403 Forbidden,并带有以下异常:"sun.security.validator. validatoreexception ">

我们在Spring和API之间有一个证书,但是证书工作得很好(应用程序返回数千个"200 ok">

但是有时候,只有这个调用返回一个"403 Forbidden"

我们已经完成了:

  • 通过代理启动Jmeter with curl(一切正常)
  • 禁用TrustStore仅用于测试(结果为ko)

这是RestTemplate代码:

SSLConnectionSocketFactory socketFactory;
socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder()
.loadTrustMaterial(ResourceUtils.getFile(this.trustStorePath), this.trustStorePassword.toCharArray())
.loadKeyMaterial(ResourceUtils.getFile(this.keyStorePath), this.keystorePassword.toCharArray(),
this.keystorePassword.toCharArray())
.build(), NoopHostnameVerifier.INSTANCE);

CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(socketFactory).setProxy(host)
.disableCookieManagement().disableRedirectHandling().build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
RestTemplate restTemplateVar = new RestTemplate(requestFactory);

这是调用:

response = this.restTemplate.getForEntity(this.host, String.class);
  • 可能是并发连接数导致的吗?
  • 为什么只使用GET和有时?
  • 最后一个:如果我们通过Httpconnection改变RestTemplate,结果可能会不同?

提前感谢

设置此属性工作良好(这取决于您的指标)

.setMaxConnTotal (1000)
.setMaxConnPerRoute (40)

CloseableHttpClient client = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.setProxy(host)
.disableCookieManagement()
.disableRedirectHandling()
.setMaxConnTotal(1000)        
.setMaxConnPerRoute(40)
.build();

最新更新