我正在尝试向以下地址发送请求。证书无效,我想忽略它。我根据我的研究在1和2上编写了以下代码,但我无法完成它。我正在使用Java 1.7。
https://api.stubhubsandbox.com/search/catalog/events/v3
代码
private static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers(){
return null;
}
public void checkClientTrusted( X509Certificate[] certs, String authType ){}
public void checkServerTrusted( X509Certificate[] certs, String authType ){}
public void checkClientTrusted(
java.security.cert.X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
}
};
public static void main(String[] args) {
TrustStrategy acceptingTrustStrategy =
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
String url = "https://api.stubhubsandbox.com/search/catalog/events/v3";
RestTemplate rest = new RestTemplate();
Map<String, String> mvm = new HashMap<String, String>();
mvm.put("Authorization", "Bearer TOKEEEEEEEN");
Object object = rest.postForObject(url, null, Object.class, mvm);
System.err.println("done");
}
UNQUESTIONING_TRUST_MANAGER
到SSLContext
。 - Michal FoksaRestTemplate
实例接受RestTemplate restTemplate = new RestTemplate(requestFactory);
的自签名证书。但是随后你使用了RestTemplate rest = new RestTemplate(); rest.postForObject(url, null, Object.class, mvm);
,这意味着实际的REST调用使用的是一个不接受自签名证书的RestTemplate
实例。如果你改用restTemplate.postForObject
,就可以使你的调用成功通过。 - manish