Sometimes we and get it by Default
Don’t just turn off the cache. Let browser handle the HTTP Request cache-control Headers
GET /welcome/ HTTP/1.1 If-Modified-Since: Mon, 26 Jul 1997 05:00:00 GMTCache-Control: no-cachePragma: no-cache
GET /welcome/ HTTP/1.1 If-Modified-Since: Mon, 26 Jul 1997 05:00:00 GMTCache-Control: no-cache no-storePragma: no-cache
By default, Spring Security sets specific cache control header values for us, without us having to configure anything such that browser will never cache HTTP responses
@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {...} @GetMapping("/default/users/{name}") public ResponseEntityThough inefficient, there is actually a good reason for this default behavior – If one user logs out and another one logs in, we don’t want them to be able to see the previous users resources. It’s much safer to not cache anything by default, and leave us to be responsible for enabling caching explicitly. More...getUserWithDefaultCaching(@PathVariable String name) { return ResponseEntity.ok(new UserDto(name)); } The resulting cache-control header will look like this: [cache-control: no-cache, no-store, max-age=0, must-revalidate]
GET /api/auth HTTP/1.1 HTTP/1.1 200 OK Cache-Control: no-cache no-store Pragma: no-cache
GET /api/welcome.json HTTP/1.1 If-Modified-Since: Mon, 29 Jun 2018 02:28:12 GMT HTTP/1.1 304 Not Modified Cache-Control: private max-age=0, must-revalidate Content-Type: text/json
GET /asset/welcome.html HTTP/1.1 HTTP/1.1 200 OK Cache-Control: public max-age=3600, must-revalidate Expires: Fri, 30 Oct 1998 14:19:41 GMT Last-Modified: Mon, 29 Jun 1998 02:28:12 GMT ETag: "3e86-410-3596fbbc" Content-Type: text/html
Browser will compare it with the server's version by sending the contents of Last-Modified or ETag, as you wanted.
max-age=0 in Cache-Control header froces browser to recheck your cached-response with the server.
must-revalidate in Cache-Control header says that the cache may not serve this content when it is stale (i.e. "expired"), but must revalidate before that. Yes, caches (and browsers) can in theory be set to serve pages even if they are stale, though the standard says they should warn the user if they do this.
no-cache used to be that you could add instead, but as users have been expecting this to behave as no-store, browsers are gradually treating them the same.More...
Governed by HTTP standard; Browser / Agents have gone through decards of evolution to make it work by defaultMDN | HTTP Conditional Get HTTP/1.1 RFC, section 14.9 Spring HTTP ETags Spring HTTP Last Modified