Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VersionResourceResolver中断CSS相对URL_Css_Spring Mvc_Spring Boot - Fatal编程技术网

VersionResourceResolver中断CSS相对URL

VersionResourceResolver中断CSS相对URL,css,spring-mvc,spring-boot,Css,Spring Mvc,Spring Boot,我在Spring Boot 1.2.2中使用VersionResourceResolver进行缓存破坏。不幸的是,它认为操纵CSS文件中指定的URI是聪明的 以Twitters引导程序为例。它在CSS文件所在目录上方的目录中查找字体 @font-face { font-family: 'Glyphicons Halflings'; src: url('../fonts/glyphicons-halflings-regular.eot'); src: url('../fonts/gl

我在Spring Boot 1.2.2中使用VersionResourceResolver进行缓存破坏。不幸的是,它认为操纵CSS文件中指定的URI是聪明的

以Twitters引导程序为例。它在CSS文件所在目录上方的目录中查找字体

@font-face {
  font-family: 'Glyphicons Halflings';

  src: url('../fonts/glyphicons-halflings-regular.eot');
  src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
}
如果没有VersionResourceResolver,它的工作方式与预期的一样:

GET http://example.com/res/css/bootstrap.min.css
GET http://example.com/res/fonts/glyphicons-halflings-regular.woff
现在,我添加VersionResourceResolver:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/res/**")
            .addResourceLocations("/resources/")
            .setCachePeriod(CACHE_SECONDS)
            .resourceChain(true)
                .addResolver(new VersionResourceResolver()                      
                .addFixedVersionStrategy(buildVersion, "/**/"))
                .addResolver(new PathResourceResolver());       
}
现在这种情况发生了:

GET http://example.com/res/085a8/css/bootstrap.min.css
GET http://example.com/res/085a8/css/fonts/glyphicons-halflings-regular.woff
那么,为什么会发生这种情况?因为有人正在篡改CSS文件中的URI:

@font-face {
  font-family: 'Glyphicons Halflings';

  src: url('085a8/../fonts/glyphicons-halflings-regular.eot');
  src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('085a8/../fonts/glyphicons-halflings-regular.woff2') format('woff2'), url('085a8/../fonts/glyphicons-halflings-regular.woff') format('woff'), url('085a8/../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
}
引导只是一个例子。在CSS资源中指定的几乎所有URI(奇怪的是,并非所有URI)都会发生这种情况。使用Spring的CssLinkResourceTransformer没有帮助。那么,我如何告诉Spring不要操纵CSS文件呢

编辑:我发现,Spring调用了CssLinkResourceTransformer。但我没有具体说明。可能是Spring Boot自动配置的

03-05 16:12:47.711 TRACE 11064 --- [nio-8080-exec-2] o.s.w.s.r.CssLinkResourceTransformer     : Transforming resource: ServletContext resource [/resources/css/bootstrap.min.css]
2015-03-05 16:12:47.716 TRACE 11064 --- [nio-8080-exec-2] o.s.w.s.r.CssLinkResourceTransformer     : Link modified: 77a6e2aa77283c05d58f4ab52102f55684b6fb04/../fonts/glyphicons-halflings-regular.eot (original: ../fonts/glyphicons-halflings-regular.eot)
2015-03-05 16:12:47.716 TRACE 11064 --- [nio-8080-exec-2] o.s.w.s.r.CssLinkResourceTransformer     : Link not modified: ../fonts/glyphicons-halflings-regular.eot?#iefix
2015-03-05 16:12:47.717 TRACE 11064 --- [nio-8080-exec-2] o.s.w.s.r.CssLinkResourceTransformer     : Link modified: 77a6e2aa77283c05d58f4ab52102f55684b6fb04/../fonts/glyphicons-halflings-regular.woff2 (original: ../fonts/glyphicons-halflings-regular.woff2)
2015-03-05 16:12:47.718 TRACE 11064 --- [nio-8080-exec-2] o.s.w.s.r.CssLinkResourceTransformer     : Link modified: 77a6e2aa77283c05d58f4ab52102f55684b6fb04/../fonts/glyphicons-halflings-regular.woff (original: ../fonts/glyphicons-halflings-regular.woff)
2015-03-05 16:12:47.719 TRACE 11064 --- [nio-8080-exec-2] o.s.w.s.r.CssLinkResourceTransformer     : Link modified: 77a6e2aa77283c05d58f4ab52102f55684b6fb04/../fonts/glyphicons-halflings-regular.ttf (original: ../fonts/glyphicons-halflings-regular.ttf)
2015-03-05 16:12:47.719 TRACE 11064 --- [nio-8080-exec-2] o.s.w.s.r.CssLinkResourceTransformer     : Link not modified: ../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular

Edit2:Spring会自动创建CssLinkResourceTransformer,如果您注册了RessourceChain-。-。请参见

此行为的原因是,如果创建ResourceChain,Spring会自动创建CssLinkResourceTransformer。你可以在他们的照片上看到。这是他们框架中的默认配置

不幸的是,在我的例子中,它破坏了CSS文件中的相对路径。为了避免CssLinkResourceTransformer的默认配置,您需要自己执行ResourceHandling。例如,这(不推荐!)。

Spring 4.1中的资源处理+ 您正在使用的VersionStrategy是为处理文件名以加载JavaScript模块的JavaScript模块加载程序定制的

由于您使用的是JavaScript模块加载器(),因此使用for CSS/images和for JS更适合您的应用程序。因此,将配置更改为此应该可以解决此问题:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    VersionResourceResolver versionResolver = new VersionResourceResolver()
            .addFixedVersionStrategy(version, "/**/*.js")
            .addContentVersionStrategy(version, "/**/*.css", "/**/*.png");

    registry.addResourceHandler("/res/**")
            .addResourceLocations("/resources/")
            .setCachePeriod(CACHE_SECONDS)
            .resourceChain(true)
                .addResolver(versionResolver);
}
请注意,您不需要注册
PathResourceResolver
CssLinkResourceTransformer
,因为这是通过检查配置为您完成的(您已经通过阅读源代码了解到了这一点)

现在,为了更好地了解该功能,我建议您阅读并查看

SPR-12669 JIRA问题 实际上,这个问题与您的用例无关,本质上不是一个bug。Guillaume创建了,我们正在讨论如何在Spring项目中更好地集成此资产管道。因此,作为一名框架开发人员,他正在为WUIC用户寻求更大的灵活性——资源处理功能本身工作正常

您所描述的解决方案并不是为应用程序设计的,因为您正在毫无理由地手动执行大量管道操作,而不是依赖Spring的默认设置


另外,请注意,我们在这里讨论的默认值是由Spring框架设置的,而不是由Spring引导设置的。Spring Boot是“唯一的”。

Daniel,请看一下我的答案——你可能会想更新这个答案,因为它向Spring开发人员发送了错误的消息。另外,您能用您正在使用的Spring框架版本更新您的问题吗?我不确定FixedVersionStrategy是否应该尝试更新相对路径(这可能是一个bug)。Brian,我用我正在使用的Spring Boot版本更新了我的原始帖子。我使用Spring框架的版本,Spring Boot“自动”提供该版本。我将更新我上面的回答,澄清这不是一个“bug”。它“破坏”了CSS文件的路径,因为您正在将FixedVersionStrategy映射到CSS文件……谢谢您的回答。我使用RequireJS加载JavaScript模块,因此需要使用VersionResourceResolver。这个解析器从最新的git提交中获取散列,并以如下方式操作我的uri:/res/js/require.js到/res/{gitshash}/js/require.js。在RequireJS的配置中,我将baseUrl设置为“/res/{gitshash}/js/”。工作完美。问题是如何处理CSS文件中的URL。见原始帖子。我已经更新了我的anwser。您可以使用多种版本策略来满足您的需要。非常感谢!这很好用:-)我昨天花了几个小时让它工作…:虽然避免FixedVersionStrategy是解决这个问题的一个方法,但我仍然认为CssLinkResourceTransformer在使用FixedVersionStrategy时不应该中断CSS URL中的相对路径。为什么FixedVersionStrategy不适合CSS文件?我为FixedVersionStrategy+CssLinkResourceTransformer的这种行为写了一个问题: