当前位置:   article > 正文

Java实现通过证书访问Https请求

Java实现通过证书访问Https请求

创建证书管理器类

  1. import java.io.FileInputStream;
  2. import java.security.KeyStore;
  3. import java.security.cert.CertificateException;
  4. import java.security.cert.X509Certificate;
  5. import javax.net.ssl.TrustManager;
  6. import javax.net.ssl.TrustManagerFactory;
  7. import javax.net.ssl.X509TrustManager;
  8. public class MyX509TrustManager implements X509TrustManager{
  9. X509TrustManager sunJSSEX509TrustManager;
  10. MyX509TrustManager(String keystoreFile,String pass) throws Exception {
  11. KeyStore ks = KeyStore.getInstance("JKS");
  12. ks.load(new FileInputStream(keystoreFile), pass.toCharArray());
  13. TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
  14. tmf.init(ks);
  15. TrustManager tms [] = tmf.getTrustManagers();
  16. for (int i = 0; i < tms.length; i++) {
  17. if (tms[i] instanceof X509TrustManager) {
  18. sunJSSEX509TrustManager = (X509TrustManager) tms[i];
  19. return;
  20. }
  21. }
  22. throw new Exception("Couldn't initialize");
  23. }
  24. @Override
  25. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  26. try {
  27. sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
  28. } catch (CertificateException excep) {
  29. excep.printStackTrace();
  30. }
  31. }
  32. @Override
  33. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  34. try {
  35. sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
  36. } catch (CertificateException excep) {
  37. excep.printStackTrace();
  38. }
  39. }
  40. @Override
  41. public X509Certificate[] getAcceptedIssuers() {
  42. return sunJSSEX509TrustManager.getAcceptedIssuers();
  43. }
  44. }

调用测试

  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.OutputStreamWriter;
  4. import java.io.PrintWriter;
  5. import java.net.URL;
  6. import javax.net.ssl.HttpsURLConnection;
  7. import javax.net.ssl.SSLContext;
  8. import javax.net.ssl.SSLSocketFactory;
  9. import javax.net.ssl.TrustManager;
  10. public class HttpsCaTest {
  11. public static void main(String[] args) throws Exception {
  12. String keystoreFile = "D:\\tomcat.keystore";
  13. String keystorePass = "ldysjhj";
  14. //设置可通过ip地址访问https请求
  15. HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
  16. // 创建SSLContext对象,并使用我们指定的信任管理器初始化
  17. TrustManager[] tm = { new MyX509TrustManager(keystoreFile,keystorePass) };
  18. SSLContext sslContext = SSLContext.getInstance("TLS");
  19. sslContext.init(null, tm, new java.security.SecureRandom());
  20. // 从上述SSLContext对象中得到SSLSocketFactory对象
  21. SSLSocketFactory ssf = sslContext.getSocketFactory();
  22. String urlStr = "https://192.168.1.10/login_queryLkBySfmc.htm";
  23. URL url = new URL(urlStr);
  24. HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  25. con.setSSLSocketFactory(ssf);
  26. con.setRequestMethod("POST"); // 设置以POST方式提交数据
  27. con.setDoInput(true); // 打开输入流,以便从服务器获取数据
  28. con.setDoOutput(true);// 打开输出流,以便向服务器提交数据
  29. //设置发送参数
  30. String param = "sfmc=测试";
  31. PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream(),"UTF-8"));
  32. out.print(param);
  33. out.flush();
  34. out.close();
  35. //读取请求返回值
  36. InputStreamReader in = new InputStreamReader(con.getInputStream(),"UTF-8");
  37. BufferedReader bfreader = new BufferedReader(in);
  38. String result = "";
  39. String line = "";
  40. while ((line = bfreader.readLine()) != null) {
  41. result += line;
  42. }
  43. System.out.println("result:"+result);
  44. }
  45. }

工具类:

  1. import javax.net.ssl.HostnameVerifier;
  2. import javax.net.ssl.SSLSession;
  3. public class NullHostNameVerifier implements HostnameVerifier{
  4. @Override
  5. public boolean verify(String hostname, SSLSession session) {
  6. return true;
  7. }
  8. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/732693
推荐阅读
相关标签
  

闽ICP备14008679号