首页 > 解决方案 > 如何在启用 Cloud Config Server 和 SSH 的 Spring Boot 应用程序中从“Resources”文件夹中读取 known_hosts 文件

问题描述

我有一个使用 @EnableConfigServer 注释启用配置服务器的 Spring Boot 应用程序。配置服务器使用 SSH 访问密钥连接到托管在 Bitbucket.org 上的 Git 存储库。由于 Spring Boot 应用程序旨在部署到 Amazon Web Services,因此我希望在项目中包含用于 SSH 的“主机”文件。Spring对此有一个属性:

spring.cloud.config.server.git.known-hosts-file

我面临的问题如下:KnownHostsFileValidator(来自spring config的一个类)尝试使用以下代码验证文件是否存在:

new File(knownHostsFile).exists()

如果我想将我的 hosts 文件包含在要打包到 jar 中的资源目录中,那么此验证将失败,因为我为 known-hosts-file 属性提供的文件名未指向“资源”目录。

我的问题是:执行“new File(filePath)”时,known-hosts-file 的属性应该如何实际指向资源目录中的文件?

标签: javaspringsshconfighosts

解决方案


推荐阅读