如何使用 Spring Boot 缓存 js、img 和 CSS 文件?
Posted
技术标签:
【中文标题】如何使用 Spring Boot 缓存 js、img 和 CSS 文件?【英文标题】:How to cache js, img and CSS file with Spring Boot? 【发布时间】:2018-07-05 23:16:07 【问题描述】:我使用带有 Spring Security 和 Thymeleaf 的 Spring Boot 2。我有 MVC 和 REST 的设置。
我正在寻找一种缓存 javascript、图像和 CSS 文件的方法。
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MultiHttpSecurityConfig
...
@Configuration
@Order(1)
public class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter
...
@Configuration
@Order(2)
public class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
auth.authenticationEventPublisher(authenticationEventPublisher).userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder);
@Override
protected void configure(HttpSecurity http) throws Exception
http.authorizeRequests()
.antMatchers("/css/**", "/webjars/**", "/js/**", "/img/**", "/")
.permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll()
.successHandler(new CustomAuthenticationSuccessHandler())
.and().logout().logoutUrl("/logout").logoutSuccessHandler(new CustomLogoutHandler())
.logoutSuccessUrl("/login").and().csrf().disable();
我创建了这个类:
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
registry.addResourceHandler("/js/**", "/webjars/**", "/img/**")
.addResourceLocations("/js/", "/webjars/", "/img/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
同样的结果,浏览器每次都会下载所有文件。
在src/main/resources
下我有:
static
css
img
js
templates
编辑:
响应头:
Accept-Ranges:bytes
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:95821
Content-Type:application/javascript
Date:Fri, 26 Jan 2018 15:40:55 GMT
Expires:0
Last-Modified:Fri, 02 Jun 2017 12:55:44 GMT
Pragma:no-cache
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
编辑 2
我在我的FormLoginWebSecurityConfigurerAdapter
类中添加了:
@Override
public void configure(WebSecurity web) throws Exception
web.ignoring().antMatchers("/css/**", "/webjars/**", "/js/**", "/img/**");
同样的结果。
好的,让它适用于资源中的 css、js
mvc 配置改为
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
registry.addResourceHandler("/resources/js/**", "/webjars/**", "/resources/img/**")
.addResourceLocations("/resources/js/", "/webjars/", "/resources/img/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
好像不需要修改 FormLoginWebSecurityConfigurerAdapter antMachers 来添加资源...
【问题讨论】:
【参考方案1】:尝试在 Spring Boot 主类 (@SpringBootApplication) 中添加以下代码
@Bean
public WebMvcConfigurerAdapter webConfigurer ()
return new WebMvcConfigurerAdapter()
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry)
registry.addResourceHandler("/static/js/**")
.addResourceLocations("/static/js/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
registry.addResourceHandler("/static/images/**")
.addResourceLocations("/static/images/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
registry.addResourceHandler("/static/css/**")
.addResourceLocations("/static/css/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
;
文件位于以下路径:
您可以添加缓存是私有的还是公共的,或者必须重新验证 setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS).cachePrivate().mustRevalidate())
【讨论】:
以上是关于如何使用 Spring Boot 缓存 js、img 和 CSS 文件?的主要内容,如果未能解决你的问题,请参考以下文章