首页 > 解决方案 > 如何在服务器 Spring Boot 应用程序中获取附加的自签名证书

问题描述

我的客户端我已在下面的 restTemplate 代码中附加了证书。使用这个休息模板我正在调用其他 API(服务器端)。如何在该服务器端获取证书

 @Bean(name="custRest")
    @Primary
    public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
        char[] password = "changeit".toCharArray();

        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

        SSLContext sslContext = SSLContextBuilder
                .create().loadKeyMaterial(new File("C:\\java\\java-certificate.der"),null,null)
                .loadTrustMaterial(ResourceUtils.getFile("C:\\java\\java-certificate.der"), null,
                        acceptingTrustStrategy)
                .build();

        HttpClient client = HttpClients.custom().setSSLContext(sslContext).build();
        return builder.requestFactory(new HttpComponentsClientHttpRequestFactory(client))
                .build();
    }

标签: javaspringspring-boot

解决方案


您可以在文章中找到为 Spring Boot 配置 HTTPS 的所有步骤: https ://www.baeldung.com/spring-boot-https-self-signed-certificate


推荐阅读