首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用spring cloud config从git-repo获取jks文件

使用spring cloud config从git-repo获取jks文件
EN

Stack Overflow用户
提问于 2018-10-28 23:02:46
回答 1查看 339关注 0票数 2

我使用的是spring cloud config server。

在我的一个应用程序中,我得到了一个存储在resources文件夹中的密钥库:

代码语言:javascript
复制
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。

我的问题是:

  1. 是否可以将.jks文件存储在git存储库中并将其提供给我的服务?
  2. 如何确保该文件只提供给相应的服务?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-07-30 05:55:45

我知道为时已晚,但我觉得同样的解决方案可能会帮助其他人实现。几周前我遇到了同样的问题,我实现了相同的代码,如下所示:在我当前的实现中,我使用以下代码来加载jks文件:

代码语言:javascript
复制
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上下文中,如下所示:

代码语言:javascript
复制
@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实现,如下所示:

代码语言:javascript
复制
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文件服务。它应该可以解决上面的问题。

如果有其他更好的方法,请纠正我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53032900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档