Sam*_*nen 11 java spring caching spring-mvc spring-security
我希望为一些静态资源启用HTTP缓存,例如图像,其访问受到Spring Security的限制.(这些资源不是安全关键,但也不应公开访问).如何避免让Spring Security添加禁用缓存的HTTP响应头?
如果我添加setCachePeriod()
到我的资源处理程序注册WebMvcConfigurerAdapter.addResourceHandlers()
如下:
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/").setCachePeriod(3600);
Run Code Online (Sandbox Code Playgroud)
仍然返回资源以及禁用缓存的以下标头:
Cache-Control: max-age=3600, must-revalidate
Expires: Mon, 04 Aug 2014 07:45:36 GMT
Pragma: no-cache
Run Code Online (Sandbox Code Playgroud)
我想避免在项目中引入任何XML配置,该项目目前仅使用Java注释配置.
有没有比扩展Spring资源处理程序更好的解决方案?
您可以使用webContentInterceptor资源来允许静态资源缓存.
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/static/*"/>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="31556926"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptor>
</mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)
使用注释来配置缓存拦截器是按照以下方式完成的.在您的Web配置类中,您可以为WebContentInterceptor
类添加bean 并将其添加到拦截器列表中.
@Bean
public WebContentInterceptor webContentInterceptor() {
WebContentInterceptor interceptor = new WebContentInterceptor();
interceptor.setCacheSeconds(31556926);
interceptor.setUseExpiresHeader(true);;
interceptor.setUseCacheControlHeader(true);
interceptor.setUseCacheControlNoStore(true);
return interceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(webContentInterceptor());
}
Run Code Online (Sandbox Code Playgroud)
请参阅此网站,了解它是如何完成的.
归档时间: |
|
查看次数: |
10357 次 |
最近记录: |