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

1

6

I am getting the above error when making post request, using spring resttemplate with mutual authentication.

@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;
    }

Code using the resttemplate is as below

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: I/O error on POST request for "https://example.com": The size of the handshake message (47942) exceeds the maximum allowed size (32768); nested exception is javax.net.ssl.SSLProtocolException: The size of the handshake message (47942) exceeds the maximum allowed size (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)

I have tried with oracle jdk 8 and 11 but same error, has anyone faced similar issue.

Brownson answered 5/12, 2020 at 17:6 Comment(1)
Yes, other people have encountered a related and similar issue. See oracle.com/java/technologies/javase/… and oracle.com/java/technologies/javase/… . Most servers shouldn't need to send excessively large handshake messages, but if yours does use the sysprop per the release notes.Unction
B
10

Based on @dave_thompson_085 comment, I made the below change and its working.

@SpringBootApplication
public class RestwrapperApplication {

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

    public static void main(String[] args) {

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

}
Brownson answered 6/12, 2020 at 7:58 Comment(1)
unfortunately im still getting the same issueCulmiferous

© 2022 - 2024 — McMap. All rights reserved.