자바 가 https 에 연결 할 때 인증서 인증 을 사용 하지 않 습 니 다.

39864 단어

   
   
   
   
  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. });




좋은 웹페이지 즐겨찾기