Feign忽略Https的SSL最佳方案(且保证负载均衡将失效)

这篇具有很好参考价值的文章主要介绍了Feign忽略Https的SSL最佳方案(且保证负载均衡将失效)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  • 同时解决Https的SSL证书验证问题和feign不支持Patch请求方法的问题

代码 1. 工具类 OkHttpUtils.java

import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

/**
 * @author Vania
 */
public class OkHttpUtils {
    /**
     * X509TrustManager instance which ignored SSL certification
     */
    public static final X509TrustManager IGNORE_SSL_TRUST_MANAGER_X509 = new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[]{};
        }
    };

    /**
     * Get initialized SSLContext instance which ignored SSL certification
     *
     * @return
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public static SSLContext getIgnoreInitedSslContext() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, new TrustManager[]{IGNORE_SSL_TRUST_MANAGER_X509}, new SecureRandom());
        return sslContext;
    }

    /**
     * Get HostnameVerifier which ignored SSL certification
     *
     * @return
     */
    public static HostnameVerifier getIgnoreSslHostnameVerifier() {
        return new HostnameVerifier() {
            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        };
    }
}

代码 2. 工具类 FeignConfiguration.java

import feign.Client;
import feign.okhttp.OkHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

@Slf4j
@Configuration
public class FeignConfiguration {

    /**
     * 解决 feign client 中https不安全的问题
     *
     * @param cachingFactory
     * @param clientFactory
     * @return
     */
    @Bean
    public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        // 此处必须为 new LoadBalancerFeignClient 否则负载均衡将失效(现象:消费者无法从注册中心获取服务提供者的ip)
        // 这个只能解决忽略https证书验证
        // return new LoadBalancerFeignClient(new Client.Default(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build().getSocketFactory(), new NoopHostnameVerifier()),
        //        cachingFactory, clientFactory);
        // 使用okhttp 解决证书验证 和 Patch请求方法不支持的问题
        return new LoadBalancerFeignClient(new OkHttpClient(new okhttp3.OkHttpClient()
                .newBuilder()
                .sslSocketFactory(OkHttpUtils.getIgnoreInitedSslContext().getSocketFactory(), OkHttpUtils.IGNORE_SSL_TRUST_MANAGER_X509)
                .hostnameVerifier(OkHttpUtils.getIgnoreSslHostnameVerifier())
                .build()),
                cachingFactory, clientFactory);
    }
}

文章来源地址https://www.toymoban.com/news/detail-647177.html

到了这里,关于Feign忽略Https的SSL最佳方案(且保证负载均衡将失效)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【SpringCloud Alibaba】Nacos服务管理与Feign负载均衡

    目录 一、微服务搭建 1.1 服务提供者与服务消费者 1.2 依赖关系   二、服务注册与负载均衡使用 2.1 Nacos 实现服务的注册与发现 2.2 Loadbalancer负载均衡、Feign声明式服务调用 2.3 示例综合实现 2.3.1 服务注册与发现测试 2.3.2 负载均衡测试  服务提供者 服务的被调用方(即:为其他微

    2024年02月03日
    浏览(37)
  • 【SpringCloud Alibaba】(四)使用 Feign 实现服务调用的负载均衡

    在上一文中,我们实现了服务的自动注册与发现功能。但是还存在一个很明显的问题:如果用户微服务和商品微服务在服务器上部署多份的话,之前的程序无法实现服务调用的负载均衡功能。 本文就带着大家一起实现服务调用的负载均衡功能 负载均衡:将原本由一台服务器

    2024年02月15日
    浏览(30)
  • HTTPS请求忽略SSL证书

    现场环境: 后端服务部署在docker内,远程调用https接口,线上报错: unable to find valid certification path to requested target 解决方案: 设置SSLSocketFactory忽略证书校验 实现案例: 使用的cn.hutool.http.HttpRequest工具类请求的数据,支持设置头部、表单、body、超时时间等关键信息 工具类 SSL

    2024年02月11日
    浏览(43)
  • RestTemplate HTTPS请求忽略SSL证书

    使用RestTemplate发送HTTPS请求的时候,出现了这样的一个问题: RestTemplate 默认不支持https协议 解决方案:         第一种是忽略认证         第二种是导入证书,比较复杂(比第一种安全)  这里说一下第一种解决方案,忽略认证 版本:Spring Boot2.x RestTemplateConfig 测试代

    2024年02月10日
    浏览(41)
  • curl 忽略https的ssl的证书验证

    今天使用curl 测试url请求出现了需要ssl证书的验证 curl的用法

    2024年02月17日
    浏览(34)
  • 请求第三方Https地址忽略SSL证书校验

    说明:个人使用记录 需要在请求之前忽略ssl协议,这里是直接使用静态方法初始化时就执行了 也需要在请求接口之前忽略SSL

    2024年04月10日
    浏览(36)
  • hutool工具进行https接口调用(忽略ssl认证)

    1.设置忽略SSl工具类 2.工具类设置

    2024年02月04日
    浏览(30)
  • java 远程调用 httpclient 调用https接口 忽略SSL认证

    httpclient 调用https接口,为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程。下面是忽略校验过程的代码类:SSLClient  然后再调用的远程get、post请求中使用SSLClient 创建Httpclient ,代码如下:

    2024年02月11日
    浏览(32)
  • Openfeign和okHttp的https请求忽略ssl证书认证

    在通过feign和okhttp请求外部接口时,出现了以下问题: Servlet.service() for servlet [dispatcherServlet] in context with path [/xxxx] threw exception [Request processing failed; nested exception is feign.RetryableException: java.security.cert.CertificateException: No subject alternative DNS name matching www.xx.xx.cn found. executing GET htt

    2024年02月07日
    浏览(39)
  • Java get/post的https请求忽略ssl证书认证

    unable to find valid certification path to requested target 工具类 使用方法

    2024年02月11日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包