2

From my client I'm querying some enums from the server. Now, I would like to cache these enums in the client as soon as they have been queried once on the server.

I tried to set Cache-Control as follows:

@CrossOrigin
@RequestMapping(value = "enums/{name}", method = RequestMethod.GET)
public List<String> getEnums( @PathVariable("name") String name, final HttpServletResponse response) {
    response.setHeader("Cache-Control", "max-age=3600");
    return myservice.findAllEnums(name);
}

The response header seems to be correctly set with Cache-control: max-age=3600. I also disabled all http headers in my security config as follows:

@Override 
protected void configure(final HttpSecurity http) throws Exception {     
http.headers().cacheControl().disable(); 
}

Unfortunately, the response is not cached in the browser. As soon as I'm querying the resource, the query is going to the server again.

Meanwhile, I removed Spring Security completly, but it still does not work. What I did not understand correctly? What else do I need to configure?

0

3 Answers 3

1

Ok, I noticed that it was working with IE, but not with Chrome. The problem with Chrome is that it sets Cache-control: max-age=0 on the request's header if you're refrehsing the page. This obviously prevents the data from being cached. I then created a simple html using a hyperlink to my application and refresehing it via this link. Thus, the cache worked as expected.

Sign up to request clarification or add additional context in comments.

Comments

0

you need to check if there is any restiction made as no-cache HTTP headers are set by Spring Security. you can get more relevant information here : How to enable HTTP response caching in Spring Boot

1 Comment

Meanwhile, I compeltly switched off Spring Security ,but it still does not work.
0

about the chrome behavior : Chrome back button doesn't respect the cache-control:no-cache header

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.