IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Spring Cloud Config启用https -> 正文阅读

[网络协议]Spring Cloud Config启用https

Spring Cloud Config启用https

前言

本篇主要整理spring cloud config如何开启https,以及微服务如何通过https去访问spring cloud config服务器上的配置文件。

项目工程

包含一个eurekaServer服务器,一个eurekaClient客户端,一个cloud config server配置中心。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-696QCpo5-1638083983368)(/Users/yuanzhihao/Library/Application Support/typora-user-images/image-20211128135316146.png)]

springcloudconfig配置中心开启https

和之前整理的文章一样,配置中心其实就是一个spring boot的工程,只要添加ssl证书以及配置证书的账号密码这些信息就可以了,这边就简单梳理下。

首先生成spring cloud config配置中心的证书configServer.keystore,关于证书生成,可以看一下之前的文章。
在这里插入图片描述

将证书放置到配置中心项目的resources目录下,并在配置文件中添加如下的配置:
在这里插入图片描述

server:
  port: 8888
  ssl:
    enabled: true # 开始ssl认证
    key-alias: configServer # 证书别名
    key-store: classpath:configServer.keystore # 证书位置
    key-store-type: JKS # 秘钥库存储类型
    key-store-password: 123456 # 秘钥库口令

spring cloud config配置中心同时也作为一个eurekaClient,需要通过https的方式注册到eureka服务器上。

添加信任库trustStore.keystore文件到项目的resource目录,信任库中包含了eureka的证书
在这里插入图片描述

在配置文件中设置信任库信息

server:
  port: 8888
  ssl:
    trust-store: classpath:trustStore.keystore # 信任库证书位置
    trust-store-type: JKS # 信任库秘钥存储类型
    trust-store-password: 123456 # 秘钥库口令

注入一个DiscoveryClientOptionalArgs的bean,同时设置其SSLContext的trustStore属性为设置的信任库的信息

/**
 * SSLContext对象 设置了信任库信息
 *
 * @author yuanzhihao
 * @since 2021/11/28
 */
@Configuration
public class SSLContextConfig {
    @Value("${server.ssl.trust-store}")
    private String trustStorePath;

    @Value("${server.ssl.trust-store-password}")
    private String trustStorePassword;

    @Bean
    public SSLContext sslContext() throws Exception {
        return SSLContextBuilder.
                create().
                loadTrustMaterial(ResourceUtils.getFile(trustStorePath), trustStorePassword.toCharArray()).
                build();
    }
}

// 通过https注册到eureka
@Bean
public DiscoveryClient.DiscoveryClientOptionalArgs discoveryClientOptionalArgs(SSLContext sslContext) {
    DiscoveryClient.DiscoveryClientOptionalArgs discoveryClientOptionalArgs = new DiscoveryClient.DiscoveryClientOptionalArgs();
    discoveryClientOptionalArgs.setSSLContext(sslContext);
    return discoveryClientOptionalArgs;
}

启动spring cloud config配置中心,发现configServer已经注册到了eureka服务器上:
在这里插入图片描述

客户端通过https去读取spring cloud config配置中心的配置文件

客户端通过https去访问spring cloud config配置中心首选需要将配置中心的证书加到信任库文件中,具体如何导入证书可以参考我之前的博客。
在这里插入图片描述

将配置文件中spring.cloud.config.uri修改为https

spring:
  application:
    name: eureka-client1
  cloud:
    config:
      name: config # 指定读取配置文件的名称
      uri: https://localhost:8888 # 指定config server的地址
      profile: default # 指定配置文件版本 默认是default

之后需要重新覆盖一下客户端与配置中心之间调用使用的RestTemplate对象,一开始我没有覆盖,导致一直是调用错误。

这边具体参考了一篇博客和spring cloud config官方文档,参考链接我放在了最后~

具体操作如下:

首先,需要自己实现一个ConfigServicePropertySourceLocator配置类,我理解是覆盖spring cloud config默认的配置

/**
 * 自定义spring config server的配置类
 *
 * @author yuanzhihao
 * @since 2021/11/28
 */
@Configuration
public class CustomConfigServiceBootstrapConfiguration {
    // spring cloud config配置文件信息
    @Autowired
    private ConfigClientProperties clientProperties;

    @Value("${server.ssl.trust-store}")
    private String trustStorePath;

    @Value("${server.ssl.trust-store-password}")
    private String trustStorePassword;

    @Bean
    public ConfigServicePropertySourceLocator configServicePropertySourceLocator() throws Exception {
        ConfigServicePropertySourceLocator configServicePropertySourceLocator =  new ConfigServicePropertySourceLocator(clientProperties);
        // 自定义spring cloud config的restTemplate 加载配置中心的信任库信息
        SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(ResourceUtils.getFile(trustStorePath), trustStorePassword.toCharArray()).build();
        CloseableHttpClient build = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(build);
        RestTemplate customRestTemplate = new RestTemplate(factory);
        configServicePropertySourceLocator.setRestTemplate(customRestTemplate);
        return configServicePropertySourceLocator;
    }
}

之后,需要在resources/META-INF目录下,创建一个名字为spring.factories的配置文件,添加如下配置来让spring加载我们自定义的配置类
在这里插入图片描述

spring.factories

org.springframework.cloud.bootstrap.BootstrapConfiguration = com.yzh.client1.config.CustomConfigServiceBootstrapConfiguration

启动客户端发现已经正常运行了,也可以访问到spring cloud config配置中心的配置文件,spring cloud config启用https OK!
在这里插入图片描述在这里插入图片描述

参考以及源码

参考地址:

  1. https://cloud.spring.io/spring-cloud-config/reference/html/#custom-rest-template
  2. https://piotrminkowski.com/2019/12/03/secure-spring-cloud-config/

源码地址:https://github.com/yzh19961031/SpringCloudDemo

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-11-29 16:38:29  更:2021-11-29 16:39:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年7日历 -2024/7/6 8:39:33-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码