微服务以https注册到Eurake

1 篇文章 0 订阅
1 篇文章 0 订阅

问题描述:
只有https://开头的请求才可以访问X-service服务,但是X-service服务注册到eurake上的时候对外暴露的请求是http://,所以使用feign无法请求到X-service,虽然X-service有把http重定向到https的功能,但是端口号却对不上,配置如下

 server:
  ssl:
    key-store-type: JKS
    key-store: classpath:keystore/ccc.jks
    key-store-password: 6cc
  port: 443
  http:
    port: 80

注册到Eurake上是http://hostname:443而不是https://hostname:443
安全端口443,不安全端口80,重定向只能把http://hostname:80重定向到https://hostname:443,但是不能把http:hostname:443重定向到https:hostname:443,也许也是可以的,但是我不知道怎么做…

解决:

eureka:
  instance:
    hostname: localhost
    securePort: ${server.port}
    securePortEnabled: true
    nonSecurePortEnabled: false
    homePageUrl: https://${eureka.instance.hostname}:${server.port}/
    statusPageUrl: https://${eureka.instance.hostname}:${server.port}/

上边的hostname是不可缺少的
还需要增加feign的配置处理,使feign绕过ssl安全验证:

@Configuration
public class SSLFeignClientConfig {
    @Bean
    @ConditionalOnMissingBean
    public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
                              SpringClientFactory clientFactory) throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext ctx = SSLContext.getInstance("SSL");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }

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

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        return new LoadBalancerFeignClient(new Client.Default(ctx.getSocketFactory(),
                (hostname, session) -> true),
                cachingFactory, clientFactory);
    }
}

feignClient使用:

@FeignClient(name="xxx-service", configuration = {SSLFeignClientConfig.class})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值