springboot 请求https的私有证书验证

一、方案描述

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

1.pom文件引入jar

java 复制代码
<dependencies>
     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Apache HttpClient - Used to request HTTP resources over the network -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
</dependencies>

2.构建一个RestTemplateConfig

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

java 复制代码
/**
 * @Author: LongGE
 * @Date: 2023-08-28
 * @Description:
 */
@Configuration
public class RestTemplateConfig {

    /**
     * 1.创建一个KeyStore,并将需要信任的证书加载到KeyStore中。示例代码如下:
     * @return
     * @throws CertificateException
     * @throws IOException
     * @throws KeyStoreException
     * @throws NoSuchAlgorithmException
     */
    @Bean
    public KeyStore createKeyStore() throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException {
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        FileInputStream inputStream =
                new FileInputStream("D:\\WorkSpace\\local\\online-project\\RequestSpringBoot\\src\\main\\resources\\my-certificate.crt");
        X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(inputStream);
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        keyStore.setCertificateEntry("my-cert", certificate);
        return keyStore;
    }

    /**
     * 2.创建一个TrustManagerFactory,使用上述创建的KeyStore来初始化它
     * @return
     * @throws CertificateException
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws IOException
     */
    @Bean
    public TrustManagerFactory createTrustManagerFactory() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
        KeyStore keyStore = createKeyStore();
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        return trustManagerFactory;
    }

    /**
     * 3.创建一个SSLContext,并使用上述创建的TrustManagerFactory来初始化它。
     * @return
     * @throws NoSuchAlgorithmException
     * @throws CertificateException
     * @throws KeyStoreException
     * @throws IOException
     * @throws KeyManagementException
     */
    @Bean
    public SSLContext createSSLContext() throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, KeyManagementException {
        TrustManagerFactory trustManagerFactory = createTrustManagerFactory();
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
        return sslContext;
    }

    /**
     * 4.创建一个HttpsURLConnectionFactory,使用上述创建的SSLContext来设置HttpsURLConnection的SSLSocketFactory。
     * @return
     * @throws CertificateException
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws KeyManagementException
     * @throws IOException
     */
    @Bean
    public RestTemplate createRestTemplate() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
        SSLContext sslContext = createSSLContext();
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setSSLContext(sslContext);
        // 创建HttpComponentsClientHttpRequestFactory
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClientBuilder.build());
        // 创建RestTemplate,并设置自定义的SSLSocketFactory
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }

}

二.使用方案

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

java 复制代码
@RestController
@RequestMapping("/TestController")
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    private String url = "https://www.houpu.com";

    private String relativePath2 = "/ResponseController/getTestMapping";


    @GetMapping("/test02")
    public String test02() {
        //发起请求
        String fullUrl2 = UriComponentsBuilder.fromHttpUrl(url).path(relativePath2).toUriString();
        String response2 = restTemplate.getForObject(fullUrl2, String.class);
        System.out.println(response2);
        return response2;
    }

}
相关推荐
千码君20168 分钟前
计算机网络:一个 IP 地址可以同时属于 A 类、B 类或 C 类吗?
网络协议·tcp/ip·计算机网络·子网划分·子网掩码·多播地址·ip分类
only_Klein2 小时前
harbor仓库搭建(配置https)
网络协议·http·docker·https·harbor
DemonAvenger4 小时前
网络超时处理与重试机制:Go最佳实践
网络协议·架构·go
yuyu_03044 小时前
电子秤利用Websocket做为Client向MES系统推送数据
网络·websocket·网络协议
w_31234544 小时前
支持多网络协议的测试工具(postman被无视版)
网络协议·测试工具·postman·调试工具
机器视觉知识推荐、就业指导7 小时前
手动开发一个TCP服务器调试工具(二):无界面 TCP 通信服最小实现
服务器·网络协议·tcp/ip
网络安全大学堂17 小时前
【网络安全入门基础教程】TCP/IP协议深入解析(非常详细)零基础入门到精通,收藏这一篇就够了
网络协议·tcp/ip·web安全·计算机·黑客·程序员·编程
结城18 小时前
深度解析 TCP 三次握手与四次挥手:从原理到 HTTP/HTTPS 的应用
tcp/ip·http·https
稚肩19 小时前
DHCP 握手原理
开发语言·网络协议
卑微的小鬼19 小时前
HTTP各个版本对比
网络·网络协议·http