spring org.springframework.core.io.UrlResource不支持https资源

时间:2018-12-03 09:05:35

标签: java spring grails ssl-certificate

配置时,grails的配置位置如下

grails.config.locations = [
    "https://ip:8443/config/Config.groovy"
]

将在日志中收到以下警告消息

 2018-11-28 19:04:22,682 WARN   ConfigurationHelper - Unable to load specified config location https://ip:8443/config/Config.groovy : File does not exist.

但是我可以直接从浏览器访问https://ip:8443/config/Config.groovy

org.codehaus.groovy.grails.commons.cfg.ConfigurationHelper.mergeInLocations()

private static void mergeInLocations(ConfigObject config, List locations, PathMatchingResourcePatternResolver resolver, ClassLoader classLoader) {

...
 def resource = resolver.getResource(location.toString())
 if(resource.exists()) {
      ...
 } else {
   LOG.warn "Unable to load specified config location $location : File does not exist."
 }

}

解析器是春季的 org.springframework.core.io.support.PathMatchingResourcePatternResolver 。和resolver.getResource(location.toString())的结果将是 org.springframework.core.io.UrlResource

和UrlResource.exists()代码类似于

public boolean exists() {
      ...
    try {
       URLConnection con = url.openConnection();
       HttpURLConnection httpCon =
                    (con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
            if (httpCon != null) {
                int code = httpCon.getResponseCode();
                if (code == HttpURLConnection.HTTP_OK) {
                    return true;
                }
                else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
                    return false;
                }
            }
    }catch(IOException ex){
       return false;
    }

}

,并且自其https以来,它将抛出 java.security.cert.CertificateException:当httpCon.getResponseCode()时,没有主题替代名称

因此UrlResource不是用于https资源吗?如果要加载https资源,该怎么办?谢谢。

1 个答案:

答案 0 :(得分:0)

请注意您描述的错误消息,我也可以重复此消息:“不存在使用者替代名称”表示您的主机名“ ip”不在证书中。尝试切换到完全限定的域名,例如“ ip.mydomain.com”或证书中编码的任何名称。 (您可以在浏览器的“页面信息”对话框上查看证书的详细信息)