我使用Spring Boot 2和Spring Security以及Thymeleaf。我有一个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 config更改为
@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来添加资源......
答案 0 :(得分:0)
尝试在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())