java连接https时禁用证书验证.


 
 
  1. import java.io.File;
  2. import java.security.cert.CertificateException;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.net.ssl.SSLContext;
  6. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  7. import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
  8. import org.apache.http.conn.ssl.TrustStrategy;
  9. import org.apache.http.impl.client.CloseableHttpClient;
  10. import org.apache.http.impl.client.HttpClients;
  11. import org.apache.http.ssl.SSLContexts;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  14. import org.springframework.security.core.annotation.AuthenticationPrincipal;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import org.springframework.web.client.RestTemplate;
  20. public String test_getssoinfo() throws Exception {
  21. TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
  22. @Override
  23. public boolean isTrusted(java.security.cert.X509Certificate[] chain,
  24. String authType) throws CertificateException {
  25. System.out.println( "in isTrusted" );
  26. // TODO 完成方法实现
  27. return true;
  28. }
  29. } ;
  30. SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
  31. .loadTrustMaterial( null, acceptingTrustStrategy)
  32. .build();
  33. SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
  34. CloseableHttpClient httpClient = HttpClients.custom()
  35. .setSSLSocketFactory(csf)
  36. .build();
  37. HttpComponentsClientHttpRequestFactory requestFactory =
  38. new HttpComponentsClientHttpRequestFactory();
  39. requestFactory.setHttpClient(httpClient);
  40. RestTemplate restTemplate = new RestTemplate(requestFactory);
  41. // RestTemplate restTemplate = new RestTemplate();
  42. String userJsonStr = restTemplate.getForObject( "https://www.xxx.yy/", String.class);
  43. return userJsonStr;
  44. }



okhttp:


 
 
  1. X509TrustManager trustManager = new X509TrustManager()
  2. {
  3. @Override
  4. public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException
  5. {
  6. }
  7. @Override
  8. public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException
  9. {
  10. }
  11. @Override
  12. public java.security.cert.X509Certificate[] getAcceptedIssuers()
  13. {
  14. StrUtil.log( "getAcceptedIssuers()");
  15. return new X509Certificate[ 0];
  16. }
  17. };
  18. SSLContext sslContext = SSLContext.getInstance( "SSL");
  19. sslContext.init( null, new X509TrustManager[] { trustManager }, new java.security.SecureRandom());
  20. SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
  21. builder.sslSocketFactory(sslSocketFactory, trustManager).hostnameVerifier( new HostnameVerifier()
  22. {
  23. @Override
  24. public boolean verify(String hostname, SSLSession session)
  25. {
  26. StrUtil.log( "verify() hostname:" + hostname + ", session:" + (session != null ? session.toString() : "null"));
  27. return true;
  28. }
  29. });




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值