1

i'm trying to use cache (caffeine) with Spring boot and im having a problem. I need to check the header "header-name" in every call but application is caching it so after first request with the right header, dont matter what header i send and the application will not check it and is just returning data from the cache, is there anyway that i can force spring to check header and then get data from cache?

 @GetMapping("/request-a")
 @Cacheable(cacheNames = "cachename", key = "#root.methodName")
 public ResponseEntity<?> makeRequest(@RequestHeader("header-name") String headerName) {


    this.authConfig.headerCheck(headerName);

    /*
    code
    */    
 }

I already used header "Cache-Control:no-cache" and didnt resolve my problem.

Thanks in advance.

Edit1: method "headerCheck" just check if its equal to another String or not null.

Marcos Oleiro
  • 27
  • 1
  • 5

1 Answers1

0

Found a solution:

Create a classe that implements HandlerInterceptor and use preHandle method.

@Component
public class CheckHeaderInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    
  // validate what you want, on error return false
  // if everything its ok, return true
  }
}

Then register the handler with:

public class WebMvcConfig implements WebMvcConfigurer {

@Autowired
private CheckHeaderInterceptor interceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(interceptor).addPathPatterns("url that you wannna use handler");
 }
}
Marcos Oleiro
  • 27
  • 1
  • 5