Java Http头将永久缓存文件,直到修改

Java Http头将永久缓存文件,直到修改,java,spring,http-caching,Java,Spring,Http Caching,我通过Spring@Controller从文件系统提供文件,我希望尽可能最好地利用浏览器缓存 以下是处理请求的方法: @ResponseBody @GetMapping(value = "/file/{f:.*}") public FileSystemResource getFile(@PathVariable("f") String fileName, HttpServletResponse response) { File file = new File("/folder/" +

我通过Spring
@Controller
从文件系统提供文件,我希望尽可能最好地利用浏览器缓存

以下是处理请求的方法:

@ResponseBody
@GetMapping(value = "/file/{f:.*}")
public FileSystemResource getFile(@PathVariable("f") String fileName, HttpServletResponse response) {

    File file = new File("/folder/" + fileName);

    response.setHeader("Cache-Control", CacheControl.maxAge(7, TimeUnit.DAYS).cachePrivate().getHeaderValue());
    response.setDateHeader("Last-Modified", file.lastModified());
    response.setHeader("ETag", "" + file.lastModified());

    return new FileSystemResource(file);
}
浏览器应永久缓存文件,除非自上次请求后文件系统中对其进行了修改

我不知道如何设置
缓存控制
过期

如何设置它们,以及需要添加哪些标题才能获得所需的行为

请注意,我还使用了
Spring-Security
,默认情况下会添加以下标题:

Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Expires:0
Pragma:no-cache

这些标题对于“普通”页面来说很好,但我不希望它们用于上面所示的方法处理
/file/**

通常使用ETag标题。从ETAG的官方Spring文档中:

ETag(实体标记)是由 HTTP/1.1兼容的web服务器,用于确定某个站点的内容更改 给定的URL

与ETag一起使用的请求头为和。这些用于使请求具有条件

编辑:要使它与SpringSecurity一起工作,您必须禁用设置这些标题。在configure方法中,您可以添加
httpSecurity.headers().cacheControl().disable()