spring - 使用 spring cloud config 从 git-repo 获取 jks 文件
问题描述
我正在使用弹簧云配置服务器。
在我的一个应用程序中,我得到了一个存储在资源文件夹中的密钥库:
server.port=443
server.ssl.enabled=true
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=12345678
server.ssl.key-alias=selfsign
server.ssl.keyStoreType=JKS
我想从包含所有常规属性(如 application.properties 等)的 repo 中获取 keystore.jks。
我的问题是:
1. 我可以将 .jks 文件存储在 git repo 上并将其提供给我的服务吗?
2. 我如何确保该文件只会发送到相应的服务?
谢谢。
解决方案
我知道已经晚了,但我觉得同样的解决方案可能会帮助其他人实施。几周前我遇到了同样的问题,我已经实现了如下相同: 在我当前的实现中,我使用下面的代码来加载 jks 文件:
Resource res = resLoader.getResource(filePath);
jksKeystore = KeyStore.getInstance("JKS");
jksKeystore.load(res.getInputStream(), clientHeaderInfo());// clientHeaderInfo() method is used to load custom header information
要处理从 Spring 云配置服务器读取的自定义路径,我们需要将完整的 http 链接设置为“server.ssl.key-store”属性。示例:http://localhost:8888/application/profile/sample.jks?useDefaultLabel=useDefaultLabel
回到自定义方面,我们需要将自定义协议解析器添加到 spring 上下文中,如下所示:
@Configuration
public class CustomProtocolResolverRegistrar implements ApplicationContextAware {
@Value("${customurl.prefix:chttp:}")
private String urlPrefix;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (applicationContext instanceof ConfigurableApplicationContext) {
final ConfigurableApplicationContext configurableApplicationContext
= (ConfigurableApplicationContext) applicationContext;
configurableApplicationContext.addProtocolResolver((location, resourceLoader)-> {
if(StringUtils.hasText(location) && location.startsWith(urlPrefix)) {
try {
return new CloudResource(location.replace(urlPrefix, ""));
}
catch (MalformedURLException e) {
logger.error("Exception while getting CloudResource : {}",e);
}
}
return null;
});
}
} }
稍后添加 CustomResource 实现,如下所示:
public class CustomResource extends UrlResource {
private String path;
private URL url;
public CustomResource(URL url) {
super(url);
this.url = url;
}
public CustomResource(String path) throws MalformedURLException {
super(path);
this.url = new URL(path);
this.path = path;
}
@Override
public InputStream getInputStream() throws IOException {
URLConnection con = this.url.openConnection();
con.setRequestProperty(org.springframework.http.HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE);
ResourceUtils.useCachesIfNecessary(con);
try {
return con.getInputStream();
}
catch (IOException e) {
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw e;
}
}
最后一步是我们需要自动配置要被弹簧加载器拾取的类:在资源文件夹下创建 META-INF/spring.factories 文件并自动配置您的类,如下所示: org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.xxx.xxx.xxx.CustomProtocolResolverRegistrar
注意:如果您使用 git 作为后端,则应正确配置基本路径以将 jks 文件提供给客户端请求。它应该可以解决上述问题。
如果有其他更好的方法,请纠正我。
推荐阅读
- r - R:预测功能错误(未标记的包)
- javascript - Chrome 扩展 打开新标签页,发送消息到新标签页
- r - 将向量拆分为具有恒定长度和恒定重叠的子向量
- python - 使用 ninja 构建 payload.bin 时出现 Python 错误
- c# - C# 窗体关闭事件冻结应用程序
- asp.net-core - 已部署的离子应用程序的 CORS 起源是什么?
- git - 子项目提交没有出现
- android - 如何显示没有 2 个导出的图像?
- conda - 在(基础)环境中意外使用“conda deactivate”后如何“conda activate base”?
- angular - 如何使用角度从 excel 中读取超链接数据?