Spring Cloud如何配置OpenFeign中调用HTTPS协议的远程接口?

Spring Cloud如何配置OpenFeign中调用HTTPS协议的远程接口?

精选文章moguli202024-12-29 1:06:0722A+A-

我们知道为了接口安全在我们的服务中经常会使用HTTPS协议将进行接口配置,而在Spring Cloud中使用OpenFeign来进行HTTPS接口调用的时候为了保证接口的安全性,需要确保客户端中能够信任相关的服务器的SSL证书。

默认情况下是会校验证书的。如果远程服务使用的是自签名证书,或者您想在开发环境中忽略证书校验,则需要对OpenFeign客户端进行额外的配置。下面我们就来介绍一种在日常开发中比较常用的证书配置方式。

配置信任所有证书

在开发和测试环境中,可以选择忽略SSL证书验证,通过配置Feign客户端来信任所有证书。这种方法适用于测试环境,生产环境不建议使用。

创建自定义的Feign配置类,配置Apache HTTP Client并信任所有证书

@Configuration
public class FeignClientConfiguration {

    @Bean
    public Client feignClient() throws Exception {
        SSLContext sslContext = SSLContextBuilder.create()
                .loadTrustMaterial((chain, authType) -> true)  // 信任所有证书
                .build();

        HttpClient httpClient = HttpClients.custom()
                .setSSLContext(sslContext)
                .setConnectionManager(new PoolingHttpClientConnectionManager())
                .build();

        return new ApacheHttpClient(httpClient);
    }
}

然后在OpenFeign接口上通过如下的配置来实现接口安全调用。

@FeignClient(name = "yourService", url = "https://example.com", configuration = FeignClientConfiguration.class)
public interface YourServiceClient {
    // 定义接口方法
}

这样配置后,Feign将会使用忽略SSL验证的HTTP客户端来发起HTTPS请求。

配置特定的信任证书

在生产环境中,通常需要验证服务器证书。可以将服务器的证书添加到客户端的信任库中,并在配置中指定。

首先,获取远程服务器的证书,并将证书导入到自定义的keystore文件中,如下所示。

keytool -import -alias server-cert -file server.crt -keystore custom-keystore.jks

然后需要在Feign中添加keystore配置。

@Bean
public Client feignClient() throws Exception {
    SSLContext sslContext = SSLContextBuilder.create()
            .loadTrustMaterial(new File("path/to/custom-keystore.jks"), "keystore-password".toCharArray())
            .build();

    HttpClient httpClient = HttpClients.custom()
            .setSSLContext(sslContext)
            .setConnectionManager(new PoolingHttpClientConnectionManager())
            .build();

    return new ApacheHttpClient(httpClient);
}

配置自定义的TrustManager

在高级场景下,可以实现自定义的TrustManager来处理证书验证逻辑。如下所示。

@Bean
public Client feignClient() throws Exception {
    SSLContext sslContext = SSLContextBuilder.create()
            .loadTrustMaterial((chain, authType) -> {
                // 自定义验证逻辑
                // 返回 true 表示信任证书,否则抛出 CertificateException
                return true;
            }).build();

    HttpClient httpClient = HttpClients.custom()
            .setSSLContext(sslContext)
            .setConnectionManager(new PoolingHttpClientConnectionManager())
            .build();

    return new ApacheHttpClient(httpClient);
}

使用application.yml配置Feign和HTTP客户端

在某些场景下,也可以通过Spring的application.yml文件来配置Feign的HTTP客户端,但对于SSL的信任配置通常还需要借助上述自定义的Bean配置方式。

# application.yml
spring:
  cloud:
    openfeign:
      httpclient:
        enable: true
  ssl:
    key-store: classpath:keystore.jks
    key-store-password: keystore-password
    key-store-type: JKS
    trust-store: classpath:truststore.jks
    trust-store-password: truststore-password

这种配置会自动应用到OpenFeign的HTTP客户端中,从而实现HTTPS的连接和认证。不过这种方式要求Spring Boot版本在2.4及以上,且使用的是spring-cloud-starter-openfeign组件。

总结

以上是几种常见的配置方式,根据不同的安全要求可以选择相应的配置方法。

点击这里复制本文地址 以上内容由莫古技术网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

莫古技术网 © All Rights Reserved.  滇ICP备2024046894号-2