I am using Microsoft Reporting API to get Azure AD Logon Activity. When i request access token,I am getting SSL handshake exception sometime. Sometime working fine without an issue Show Url to get access token : https://login.microsoftonline.com/test.onmicrooft.com/oauth2/token?api-version=1.0 Stacktrace
[10:03:48:293]|[12-23-2020]|[SYSERR]|[INFO]|[931]: at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:992)|
Answers
All replies
The exception logs will look like this. I have
already posted code fix to bypass SSL matching in earlier post. Unfortunately, that fix works in Now there are two ways, you can utilize the imported certificate from server. Either add certificate to the JDK cacerts store; or pass certificate information in JVM aruguments. 1) Import certificate to JDK cacert store
Now create HTTP client as given: public HttpClient createTlsV2HttpClient() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException { SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); SSLConnectionSocketFactory f = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", f) .build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry); CloseableHttpClient client = HttpClients .custom() .setSSLSocketFactory(f) .setConnectionManager(cm) .build(); return client; } Notice the code : 2) Pass certificate information in JVM aruguments
Now create HTTP client as given: public HttpClient createTlsV2HttpClient() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException { SSLContext sslContext = SSLContexts.createSystemDefault(); SSLConnectionSocketFactory f = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", f) .build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry); CloseableHttpClient client = HttpClients .custom() .setSSLSocketFactory(f) .setConnectionManager(cm) .build(); return client; } Notice the code : Summary
Drop me your questions in comments section. Happy Learning !! Join 8000+ Awesome Developers, Like YOU!What is remote host closed connection during handshake?[SOLVED] TLS 1.2 – SSLHandshakeException: Remote host closed connection during handshake. SSLHandshakeException appear in logs when there is some error occur while validating the certificate installed in client machine with certificate on server machine.
How do I fix SSLHandshakeException?SSLHandshakeException can be resolved 2 ways.. Incorporating SSL. Get the SSL (by asking the source system administrator, can also be downloaded by openssl command, or any browsers downloads the certificates) ... . Ignoring SSL.. |