I am using old tech stack for maven webapp because of architecture: Java - 1.6, httpClient - 4.2.3. (DefaultHttpClient) I am trying redirect from speed-link to another site within signed form. Process includes two part: firstly I am sending https post request for getting JWT token, Secondly I am sending https get request with the specific token. But when I click the redirect link which gets token, I am getting javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated error.
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
HttpPost postRequest = new HttpPost(endpointAddress);
StringEntity input = new StringEntity(jo.toString(), "UTF-8");
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
HttpEntity httpEntity = response.getEntity();
String responseString = EntityUtils.toString(httpEntity);
JSONObject resultObj = new JSONObject(responseString);
String tokenId = resultObj.getString("token");
}catch(Exception e) {
e.printStackTrace();
}finally {
httpClient.getConnectionManager().shutdown();
}
I found a solution from similar asked questions, but it did not worked in my case.
private DefaultHttpClient getDefaultHttpClient(DefaultHttpClient httpClient) throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = null;
sslContext = SSLContext.getInstance("SSL");
// set up a TrustManager that trusts everything
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
System.out.println("getAcceptedIssuers =============");
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
System.out.println("checkClientTrusted =============");
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
System.out.println("checkServerTrusted =============");
}
} }, new SecureRandom());
final SSLSocketFactory theSslSocketFactory = new SSLSocketFactory(sslContext);
final Scheme theHttpsScheme = new Scheme("https", theSslSocketFactory, 443);
httpClient.getConnectionManager().getSchemeRegistry().register(theHttpsScheme);
return httpClient;
}