握手消息的大小(x)超过了允许的最大大小(32768):spring boot restemplate

hof1towb  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(610)

我在使用springrestemplate和相互认证进行post请求时遇到了上述错误。

  1. @Bean
  2. public RestTemplate restTemplate() throws UnrecoverableKeyException,
  3. NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException, CertificateException {
  4. KeyStore clientStore = KeyStore.getInstance("PKCS12");
  5. clientStore.load(new FileInputStream(pfxFile), pfxPass.toCharArray());
  6. SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
  7. sslContextBuilder.useProtocol("TLS");
  8. sslContextBuilder.loadKeyMaterial(clientStore, pfxPass.toCharArray());
  9. sslContextBuilder.loadTrustMaterial(new TrustSelfSignedStrategy());
  10. SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
  11. CloseableHttpClient httpClient = HttpClients.custom()
  12. .setSSLSocketFactory(sslConnectionSocketFactory)
  13. .build();
  14. HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
  15. requestFactory.setConnectTimeout(Integer.parseInt(timeOut)); // 10 seconds
  16. requestFactory.setReadTimeout(Integer.parseInt(timeOut)); // 10 seconds
  17. RestTemplate restTemplate = new RestTemplate(requestFactory);
  18. restTemplate.setInterceptors( Collections.singletonList(new RequestResponseLoggingInterceptor()));
  19. return restTemplate;
  20. }

使用restemplate的代码如下

  1. public ResponseEntity<OauthResponse> getOauthToken(String clientScope,
  2. String BasicAuthUser,String BasicAuthPass){
  3. String accessToken = Base64.getEncoder().encodeToString((BasicAuthUser+":"+BasicAuthPass).getBytes());
  4. HttpHeaders headers = new HttpHeaders();
  5. headers.set("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
  6. // headers.set("apikey", BasicAuthUser);
  7. // headers.set("Authorization", "Basic "+accessToken);
  8. HttpEntity<?> entity = new HttpEntity<>(headers);
  9. UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(oauthUrl)
  10. .queryParam("grant_type", "client_credentials")
  11. .queryParam("scope", clientScope);
  12. return restTemplate.exchange(
  13. builder.toUriString(),
  14. HttpMethod.POST,
  15. entity,
  16. OauthResponse.class);
  17. }

org.springframework.web.client.resourceaccessexception:发布请求时出现i/o错误“https://example.com“:握手消息的大小(47942)超过了允许的最大大小(32768);嵌套的异常是javax.net.ssl.sslprotocolexception:握手消息的大小(47942)超过了允许的最大大小(32768)

  1. at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:748)
  2. at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
  3. at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583)
  4. at in.co.confluent.gopayhrfc.restwrapper.hdfcapi.service.OauthService.getOauthToken(OauthService.java:38)
  5. at in.co.confluent.gopayhrfc.restwrapper.hdfcapi.service.OauthServiceTest.checkOauthServiceResponse(OauthServiceTest.java:26)

我试过使用oraclejdk8和11,但同样的错误,有人遇到过类似的问题。

z2acfund

z2acfund1#

基于@dave\u thompson\u085的评论,我做了以下的修改并生效了。

  1. @SpringBootApplication
  2. public class RestwrapperApplication {
  3. static{
  4. System.setProperty("jdk.tls.maxHandshakeMessageSize", "50000");
  5. }
  6. public static void main(String[] args) {
  7. SpringApplication.run(RestwrapperApplication.class, args);
  8. }
  9. }

相关问题