握手消息的大小(X)超过最大允许大小(32768):spring boot resttemplate

The size of the handshake message (X) exceeds the maximum allowed size (32768):spring boot resttemplate

我在发出 post 请求时遇到上述错误,使用 spring resttemplate 进行相互身份验证。

@Bean
    public RestTemplate restTemplate() throws UnrecoverableKeyException,
            NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException, CertificateException {
        KeyStore clientStore = KeyStore.getInstance("PKCS12");
        clientStore.load(new FileInputStream(pfxFile), pfxPass.toCharArray());

        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
        sslContextBuilder.useProtocol("TLS");
        sslContextBuilder.loadKeyMaterial(clientStore, pfxPass.toCharArray());
        sslContextBuilder.loadTrustMaterial(new TrustSelfSignedStrategy());

        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(sslConnectionSocketFactory)
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        requestFactory.setConnectTimeout(Integer.parseInt(timeOut)); // 10 seconds
        requestFactory.setReadTimeout(Integer.parseInt(timeOut)); // 10 seconds
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        restTemplate.setInterceptors( Collections.singletonList(new RequestResponseLoggingInterceptor()));

        return restTemplate;
    }

使用resttemplate的代码如下

public ResponseEntity<OauthResponse> getOauthToken(String clientScope,
                                                       String BasicAuthUser,String BasicAuthPass){


        String accessToken = Base64.getEncoder().encodeToString((BasicAuthUser+":"+BasicAuthPass).getBytes());
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
//        headers.set("apikey", BasicAuthUser);
//        headers.set("Authorization", "Basic "+accessToken);

        HttpEntity<?> entity = new HttpEntity<>(headers);

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(oauthUrl)
                .queryParam("grant_type", "client_credentials")
                .queryParam("scope", clientScope);

        return restTemplate.exchange(
                builder.toUriString(),
                HttpMethod.POST,
                entity,
                OauthResponse.class);
    }

org.springframework.web.client.ResourceAccessException:POST 请求“https://example.com”时出现 I/O 错误:握手消息 (47942) 的大小超出允许的最大值尺寸(32768);嵌套异常是 javax.net.ssl.SSLProtocolException:握手消息的大小 (47942) 超过了最大允许大小 (32768)

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

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

根据@dave_thompson_085 的评论,我进行了以下更改并使其生效。

@SpringBootApplication
public class RestwrapperApplication {

    static{
        
        System.setProperty("jdk.tls.maxHandshakeMessageSize", "50000");
    }

    public static void main(String[] args) {

        SpringApplication.run(RestwrapperApplication.class, args);
    }

}