java – 如何使用Spring MVC和Spring Security为资源处理程序启
发布时间:2020-12-14 17:44:11 所属栏目:Java 来源:网络整理
导读:我希望为某些静态资源(如图像)启用HTTP缓存,Spring Security对此进行了限制. (这些资源不是安全性关键,但也不应公开访问).如何避免Spring Security添加禁用缓存的HTTP响应头? 如果我将SetCachePeriod()添加到WebMvcConfigurerAdapter.addResourceHandlers()
我希望为某些静态资源(如图像)启用HTTP缓存,Spring Security对此进行了限制. (这些资源不是安全性关键,但也不应公开访问).如何避免Spring Security添加禁用缓存的HTTP响应头?
如果我将SetCachePeriod()添加到WebMvcConfigurerAdapter.addResourceHandlers()中的资源处理程序注册中,如下所示: registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/").setCachePeriod(3600); 资源仍然返回,并带有以下标题,禁用高速缓存: Cache-Control: max-age=3600,must-revalidate Expires: Mon,04 Aug 2014 07:45:36 GMT Pragma: no-cache 我想避免在项目中引入任何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> 使用注释配置缓存拦截器是按照以下方式完成的. @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()); } 参考this site看看它是如何完成的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |