How to enable HTTP response caching in Spring Boot

前端 未结 8 1897
我寻月下人不归
我寻月下人不归 2020-11-27 12:17

I have implemented a REST server using Spring Boot 1.0.2. I\'m having trouble preventing Spring from setting HTTP headers that disable HTTP caching.

My controller is

8条回答
  •  失恋的感觉
    2020-11-27 13:11

    If you don't care to have your static resources authenticated, you could do this:

    import static org.springframework.boot.autoconfigure.security.servlet.PathRequest.toStaticResources;
    
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ...
        @Override
        public void configure(WebSecurity webSecurity) throws Exception {
            webSecurity
                    .ignoring()
                    .requestMatchers(toStaticResources().atCommonLocations());
        }
    ...
    }
    

    and in your application.properties:

    spring.resources.cache.cachecontrol.max-age=43200
    

    See ResourceProperties.java for more properties that can be set.

提交回复
热议问题