我使用的是spring cloud config server。
在我的一个应用程序中,我得到了一个存储在resources文件夹中的密钥库:
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等)的存储库中获取keystore.jks。
我的问题是:
谢谢。
发布于 2021-07-30 05:55:45
我知道为时已晚,但我觉得同样的解决方案可能会帮助其他人实现。几周前我遇到了同样的问题,我实现了相同的代码,如下所示:在我当前的实现中,我使用以下代码来加载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 cloud配置服务器读取的自定义路径,我们需要将完整的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;
}
}com.xxx.xxx.xxx.CustomProtocolResolverRegistrar
注意:如果您使用git作为后端,那么应该正确配置basepath,以便为客户端请求提供jks文件服务。它应该可以解决上面的问题。
如果有其他更好的方法,请纠正我。
https://stackoverflow.com/questions/53032900
复制相似问题