itsource

Spring Boot에서 Cache-Control 헤더를 정적 리소스에 추가하려면 어떻게 해야 합니까?

mycopycode 2023. 2. 26. 09:45
반응형

Spring Boot에서 Cache-Control 헤더를 정적 리소스에 추가하려면 어떻게 해야 합니까?

추가 방법Cache-ControlSpring Boot의 HTTP 헤더는 스태틱리소스용입니까?

응용 프로그램에서 헤더를 올바르게 쓰는 filter-component를 사용하려고 했지만Cache-Control헤더를 덮어씁니다.

@Component
public class CacheBustingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
                                              throws IOException, ServletException {

        HttpServletResponse httpResp = (HttpServletResponse) resp;
        httpResp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        httpResp.setHeader("This-Header-Is-Set", "no-cache, no-store, must-revalidate");
        httpResp.setHeader("Expires", "0");

        chain.doFilter(req, resp);
    }

브라우저에 표시되는 내용은 다음과 같습니다.

Cache-Control:no-store
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0

내가 원하는 것은:

Cache-Control:no-cache, no-store, must-revalidate
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0

이는 Spring Security에 의해 발생합니다.이것에 의해, 캐시를 완전하게 무효로 하기 위해서입니다.그래서 우리는 두 가지 일을 해야 합니다.

  1. 정적 리소스에 대한 스프링 보안 사용 안 함
  2. 정적 리소스 캐시 처리 사용

현재 버전의 Spring Boot에서는 application.properties 설정에서 이 동작을 변경할 수 있습니다.

일부 리소스에 대해 스프링 보안을 비활성화합니다.

# Comma-separated list of paths to exclude from the default secured 
security.ignored=/myAssets/**

정적 리소스에 대한 캐시 헤더 전송 사용:

# Enable HTML5 application cache manifest rewriting.
spring.resources.chain.html-application-cache=true

# Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled.
spring.resources.chain.enabled=true
# Enable the content Version Strategy.
spring.resources.chain.strategy.content.enabled=true 
# Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.content.paths=/** 

# Locations of static resources.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

그게 다예요.이제 Spring은 정적 파일이 변경되었는지 확인하고 스마트한 응답(If-Modiffied-Since 등)을 전송할 수 있으며 앱 캐시도 다시 씁니다.

또한 일부 리소스에 콘텐츠 기반 버전을 사용하지 않는 이유가 있는 경우 대체 FixedVersion 전략을 사용하여 Configuration에서 버전을 명시적으로 설정할 수 있습니다.

#Enable the fixed Version Strategy.
spring.resources.chain.strategy.fixed.enabled=false 
# Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.fixed.paths= 
# Version string to use for the Version Strategy.
spring.resources.chain.strategy.fixed.version= 

문서 상세보기

설명서에 따르면ResourceHandlerRegistry그것은 꽤 쉽다.(현재는 관련 코드는 없습니다.)

스태틱 리소스를 구성하는 장소에서는addResourceHandler메서드, 반환됩니다.ResourceHandlerRegistration물건.

여기서 setCacheControl 메서드를 사용할 수 있습니다.캐시 제어 objct를 설정 및 설정해야 합니다.

이것은 봄 4.2 이후입니다.그렇지 않으면 다음과 같이 해야 합니다.

@Configuration
@EnableWebMvc
@ComponentScan("my.packages.here")
public class WebConfig extends WebMvcConfigurerAdapter {


   @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").setCachePeriod(0);
    }

}

리소스 기본 캐시 헤더를 제어하는 속성은 다음과 같습니다.

spring.resources.cache.cachecontrol.max-age: 3600

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

스프링 부트에는 http 리소스를 캐싱하는 다양한 방법이 있습니다.스프링 부트 2.1.1 및 스프링 보안 5.1.1을 사용합니다.

1. 리소스 핸들러 인 코드(UNTESTED)를 사용하는 리소스의 경우:

이 방법으로 사용자 지정된 리소스 확장을 추가할 수 있습니다.

registry.addResourceHandler

리소스를 가져올 uri 경로를 추가합니다.

.addResourceLocations

리소스가 있는 파일 시스템의 위치를 설정하기 위한 것입니다(지정된 경로는 클래스 경로와의 상대 경로이지만 파일:://의 절대 경로도 가능합니다).

.setCacheControl

캐시 헤더를 설정하기 위한 것입니다(자체 설명).

자원 체인 및 리졸바는 옵션입니다(이 경우 기본값 그대로).

@Configuration
public class CustomWebMVCConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")
            .setCacheControl(CacheControl.noStore()
                    .mustRevalidate())
            .setCacheControl(CacheControl.noCache())
            .resourceChain(true)
            .addResolver(new PathResourceResolver());
    }
}

2. 응용 프로그램 속성 구성 파일을 사용하는 리소스

상기와 같으며, 특정 패턴을 제외하고, 현재는 config로 되어 있습니다.이 설정은 나열된 정적 위치에 있는 모든 리소스에 적용됩니다.

spring.resources.cache.cachecontrol.no-store=true
spring.resources.cache.cachecontrol.must-revalidate=true
spring.resources.cache.cachecontrol.no-cache=true

3. 컨트롤러 레벨에서

여기서의 응답은 컨트롤러 메서드에 파라미터로 삽입된HttpServletResponse 입니다.

response.setHeader(HttpHeaders.CACHE_CONTROL,
            "no-cache, must-revalidate, no-store");
response.setHeader("Expires", "0");

.@Override public void addResourceHandlers(ResourceHandlerRegistry registry) {}될 수 있습니다.그것들은 다른 사람들에게도 유용할 수 있다.

Spring Web MVC를 사용하는 기본 Spring Boot 2.4 응용 프로그램의 다음 디렉토리 구조를 가정합니다.

src/main/resources/
|- static/
  |- res/
    |- css/
    |- js/
  |- images/
  |- favicon.ico

그리고 css, js, images, favicon 캐싱을 추가하고 싶습니다.다음으로 Configuration은 다음과 같습니다.

import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

@Configuration
public class CacheStaticResourcesConfiguration implements WebMvcConfigurer {

    /**
     * We provide a custom configuration which resolves URL-Requests to static files in the
     * classpath (src/main/resources directory).
     *
     * This overloads a default configuration retrieved at least partly from
     * {@link WebProperties.Resources#getStaticLocations()}.
     * @param registry ResourceHandlerRegistry
     */
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        /*
         * BE AWARE HERE:
         *
         * .addResourceHandler(): URL Paths
         * .addResourceLocations(): Paths in Classpath to look for file
         *   root "/" refers to src/main/resources
         *   For configuration example, see:
         *     org.springframework.boot.autoconfigure.web.WebProperties.Resources().getStaticLocations()
         *
         * .addResourceLocations("classpath:/static/")
         *   =>
         *      addResourceHandler("/**")
         *      => GET /res/css/main.css
         *         => resolved as: "classpath:/static/res/css/main.css"
         *      BUT
         *      addResourceHandler("/res/**")
         *      => GET /res/css/main.css
         *            (spring only appends the ** to the value from
         *             addResourceLocations())
         *         => resolved as: "classpath:/static/css/main.css"
         */

        registry
                .addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/static/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS)
                        .noTransform()
                        .mustRevalidate());

        registry
                .addResourceHandler("/res/**")
                // trailing slash is important!
                .addResourceLocations("classpath:/static/res/")
                .setCacheControl(CacheControl.maxAge(7, TimeUnit.DAYS)
                        .noTransform()
                        .mustRevalidate());

        registry
                .addResourceHandler("/images/**")
                // trailing slash is important!
                .addResourceLocations("classpath:/static/images/")
                .setCacheControl(CacheControl.maxAge(7, TimeUnit.DAYS)
                        .noTransform()
                        .mustRevalidate());
    }

}

말레랑크, 정답이야.다만, 이 실장에는, 1개의 문제가 있습니다.

다음 코드는 첫 번째 요구에서 올바른 캐시 제어 헤더를 제공하지만 이후 304(Not-Modified)를 반환하는 요구에서는 스프링 보안에 의해 설정된 기본 캐시 제어 헤더가 반환되지 않습니다.{code}

public void addResourceHandlers(ResourceHandlerRegistry registry) {         
    registry.addResourceHandler("/resources/**").setCacheControl(CacheControl.maxAge(10, TimeUnit.SECONDS));
}

저는 이 문제를 스프링 팀에 제기했습니다.https://jira.spring.io/browse/SPR-15133를 참조하십시오.여기에는 다음과 같은 응답이 있습니다.「이제는, 애플리케이션 전체에 대해서 시큐러티 캐시 제어 헤더를 무효로 하는 것이 아닙니다.특정 패스(리소스 처리, 여기서)에 대해서, 적절한 방법을, 그 문제의 코멘트로 설명하고 있습니다.「회피책」섹션을 참조해 주세요.」

spring boot 1.3.3에서는 malenc answer를 사용하여 404의 답변을 받았습니다.리소스 위치를 추가하여 수정할 수 있습니다.

@Configuration
public class HttpClientConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS))
                .addResourceLocations("/");
    }
}

이 때 더해서 쓸 수도 있어요.Cache-Control다음 중 하나:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    WebContentInterceptor interceptor = new WebContentInterceptor();
    interceptor.addCacheMapping(CacheControl.maxAge(60, TimeUnit.SECONDS)
      .noTransform()
      .mustRevalidate(), "/static/**");
    registry.addInterceptor(interceptor);
}

그러면 리소스 위치를 지정할 필요가 없습니다.

https://www.baeldung.com/spring-mvc-cache-headers#cache-interceptors

언급URL : https://stackoverflow.com/questions/33214501/how-to-add-cache-control-header-to-static-resource-in-spring-boot

반응형