• springboot 请求https的私有证书验证


    一、方案描述

    我这里采用RestTemplate的方式调用https请求,请求第三方接口获取数据,证书由第三方私自签发的证书,我们构建的是一个springboot的API项目。

    1.pom文件引入jar

    1. org.springframework.boot
    2. spring-boot-starter-web
    3. org.apache.httpcomponents
    4. httpclient
    5. org.springframework.boot
    6. spring-boot-configuration-processor
    7. true

    2.构建一个RestTemplateConfig

    构建RestTemplateConfig为了初始化RestTemplate让它具备验证证书功能。

    1. /**
    2. * @Author: LongGE
    3. * @Date: 2023-08-28
    4. * @Description:
    5. */
    6. @Configuration
    7. public class RestTemplateConfig {
    8. /**
    9. * 1.创建一个KeyStore,并将需要信任的证书加载到KeyStore中。示例代码如下:
    10. * @return
    11. * @throws CertificateException
    12. * @throws IOException
    13. * @throws KeyStoreException
    14. * @throws NoSuchAlgorithmException
    15. */
    16. @Bean
    17. public KeyStore createKeyStore() throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException {
    18. CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    19. FileInputStream inputStream =
    20. new FileInputStream("D:\\WorkSpace\\local\\online-project\\RequestSpringBoot\\src\\main\\resources\\my-certificate.crt");
    21. X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(inputStream);
    22. KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    23. keyStore.load(null, null);
    24. keyStore.setCertificateEntry("my-cert", certificate);
    25. return keyStore;
    26. }
    27. /**
    28. * 2.创建一个TrustManagerFactory,使用上述创建的KeyStore来初始化它
    29. * @return
    30. * @throws CertificateException
    31. * @throws NoSuchAlgorithmException
    32. * @throws KeyStoreException
    33. * @throws IOException
    34. */
    35. @Bean
    36. public TrustManagerFactory createTrustManagerFactory() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
    37. KeyStore keyStore = createKeyStore();
    38. TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    39. trustManagerFactory.init(keyStore);
    40. return trustManagerFactory;
    41. }
    42. /**
    43. * 3.创建一个SSLContext,并使用上述创建的TrustManagerFactory来初始化它。
    44. * @return
    45. * @throws NoSuchAlgorithmException
    46. * @throws CertificateException
    47. * @throws KeyStoreException
    48. * @throws IOException
    49. * @throws KeyManagementException
    50. */
    51. @Bean
    52. public SSLContext createSSLContext() throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, KeyManagementException {
    53. TrustManagerFactory trustManagerFactory = createTrustManagerFactory();
    54. SSLContext sslContext = SSLContext.getInstance("TLS");
    55. sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
    56. return sslContext;
    57. }
    58. /**
    59. * 4.创建一个HttpsURLConnectionFactory,使用上述创建的SSLContext来设置HttpsURLConnection的SSLSocketFactory。
    60. * @return
    61. * @throws CertificateException
    62. * @throws NoSuchAlgorithmException
    63. * @throws KeyStoreException
    64. * @throws KeyManagementException
    65. * @throws IOException
    66. */
    67. @Bean
    68. public RestTemplate createRestTemplate() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
    69. SSLContext sslContext = createSSLContext();
    70. HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    71. httpClientBuilder.setSSLContext(sslContext);
    72. // 创建HttpComponentsClientHttpRequestFactory
    73. HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    74. requestFactory.setHttpClient(httpClientBuilder.build());
    75. // 创建RestTemplate,并设置自定义的SSLSocketFactory
    76. RestTemplate restTemplate = new RestTemplate(requestFactory);
    77. return restTemplate;
    78. }
    79. }

    二.使用方案

    这样构建好的RestTemplate,我们在Controller或者Service就可以通过@Autowried注解引入。

    1. @RestController
    2. @RequestMapping("/TestController")
    3. public class TestController {
    4. @Autowired
    5. private RestTemplate restTemplate;
    6. private String url = "https://www.houpu.com";
    7. private String relativePath2 = "/ResponseController/getTestMapping";
    8. @GetMapping("/test02")
    9. public String test02() {
    10. //发起请求
    11. String fullUrl2 = UriComponentsBuilder.fromHttpUrl(url).path(relativePath2).toUriString();
    12. String response2 = restTemplate.getForObject(fullUrl2, String.class);
    13. System.out.println(response2);
    14. return response2;
    15. }
    16. }

  • 相关阅读:
    机器学习之分类回归模型(决策数、随机森林)
    【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
    [Java反序列化]—Shiro反序列化(二)
    10年测试经验,在35岁的生理年龄面前,一文不值
    python rb读取文件 base64加密 byte.decode解密,base64解密
    归并排序和快速排序的两种实现
    小学期-中期总结报告
    渗透测试tomcat错误信息泄露解决办法
    计算机网络文章荟萃
    【数据结构】线性表(四)双向链表的各种操作(插入、删除、查找、修改、遍历打印)
  • 原文地址:https://blog.csdn.net/gelong_bokewang/article/details/132731615