|
1 | 1 | package com.huntly.server.config; |
2 | 2 |
|
3 | | -import java.util.concurrent.TimeUnit; |
4 | | -import org.springframework.boot.autoconfigure.web.WebProperties; |
| 3 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| 4 | +import org.springframework.context.annotation.Bean; |
5 | 5 | import org.springframework.context.annotation.Configuration; |
6 | 6 | import org.springframework.http.CacheControl; |
7 | | -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
8 | | -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
9 | | - |
10 | | -@Configuration |
11 | | -public class WebResourceCacheConfig implements WebMvcConfigurer { |
| 7 | +import org.springframework.web.filter.OncePerRequestFilter; |
12 | 8 |
|
13 | | - private final WebProperties.Resources resources; |
| 9 | +import javax.servlet.FilterChain; |
| 10 | +import javax.servlet.ServletException; |
| 11 | +import javax.servlet.http.HttpServletRequest; |
| 12 | +import javax.servlet.http.HttpServletResponse; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.concurrent.TimeUnit; |
14 | 15 |
|
15 | | - public WebResourceCacheConfig(WebProperties webProperties) { |
16 | | - this.resources = webProperties.getResources(); |
| 16 | +@Configuration |
| 17 | +public class WebResourceCacheConfig { |
| 18 | + |
| 19 | + @Bean |
| 20 | + public FilterRegistrationBean<OncePerRequestFilter> staticResourceCacheFilter() { |
| 21 | + FilterRegistrationBean<OncePerRequestFilter> registration = new FilterRegistrationBean<>(); |
| 22 | + registration.setFilter(new OncePerRequestFilter() { |
| 23 | + @Override |
| 24 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, |
| 25 | + FilterChain filterChain) throws ServletException, IOException { |
| 26 | + String path = request.getRequestURI(); |
| 27 | + String cacheHeader = getCacheControlHeader(path); |
| 28 | + if (cacheHeader != null) { |
| 29 | + response.setHeader("Cache-Control", cacheHeader); |
| 30 | + } |
| 31 | + filterChain.doFilter(request, response); |
| 32 | + } |
| 33 | + }); |
| 34 | + registration.addUrlPatterns("/*"); |
| 35 | + registration.setOrder(1); |
| 36 | + return registration; |
17 | 37 | } |
18 | 38 |
|
19 | | - @Override |
20 | | - public void addResourceHandlers(ResourceHandlerRegistry registry) { |
21 | | - String[] staticLocations = resources.getStaticLocations(); |
22 | | - CacheControl noCache = CacheControl.noCache(); |
23 | | - CacheControl weekCache = CacheControl.maxAge(7, TimeUnit.DAYS).cachePublic(); |
24 | | - CacheControl dayCache = CacheControl.maxAge(1, TimeUnit.DAYS).cachePublic(); |
25 | | - |
26 | | - registry.addResourceHandler("/index.html") |
27 | | - .addResourceLocations(staticLocations) |
28 | | - .setCacheControl(noCache); |
29 | | - |
30 | | - registry.addResourceHandler("/static/media/**") |
31 | | - .addResourceLocations(staticLocations) |
32 | | - .setCacheControl(dayCache); |
33 | | - |
34 | | - registry.addResourceHandler("/static/js/**", "/static/css/**") |
35 | | - .addResourceLocations(staticLocations) |
36 | | - .setCacheControl(weekCache); |
37 | | - |
38 | | - registry.addResourceHandler( |
39 | | - "/*.png", |
40 | | - "/*.jpg", |
41 | | - "/*.jpeg", |
42 | | - "/*.gif", |
43 | | - "/*.webp", |
44 | | - "/*.svg", |
45 | | - "/*.ico" |
46 | | - ) |
47 | | - .addResourceLocations(staticLocations) |
48 | | - .setCacheControl(dayCache); |
| 39 | + private String getCacheControlHeader(String path) { |
| 40 | + // index.html 不缓存 |
| 41 | + if (path.endsWith("/index.html") || path.equals("/")) { |
| 42 | + return CacheControl.noCache().getHeaderValue(); |
| 43 | + } |
| 44 | + // 带 hash 的 js/css 文件长期缓存 |
| 45 | + if (path.startsWith("/static/js/") || path.startsWith("/static/css/")) { |
| 46 | + return CacheControl.maxAge(7, TimeUnit.DAYS).cachePublic().getHeaderValue(); |
| 47 | + } |
| 48 | + // 媒体文件缓存1天 |
| 49 | + if (path.startsWith("/static/media/")) { |
| 50 | + return CacheControl.maxAge(1, TimeUnit.DAYS).cachePublic().getHeaderValue(); |
| 51 | + } |
| 52 | + // 根目录图片缓存1天 |
| 53 | + if (path.matches("^/[^/]+\\.(png|jpg|jpeg|gif|webp|svg|ico)$")) { |
| 54 | + return CacheControl.maxAge(1, TimeUnit.DAYS).cachePublic().getHeaderValue(); |
| 55 | + } |
| 56 | + return null; |
49 | 57 | } |
50 | 58 | } |
0 commit comments