簡體   English   中英

Tomcat閥強制特定的Cache-Control標頭

[英]Tomcat valve forcing specific Cache-Control headers

我想在響應中正確設置Cache-Control和ETag標頭。 為此,我已通過Spring安全配置禁用了請求緩存:

httpSecurity.headers().cacheControl().disable();

然后,當返回響應時:

ResponseEntity.ok()
        .header("Cache-Control", "max-age=60")
        .header("ETag", "my-tag")
        .build()

這似乎在某種意義上起作用,即未返回默認的spring security緩存控制標頭(默認情況下,我認為它們返回“ no-cache,no-store,max-age = 0,must-revalidate”),並且我的標頭是出現在回應中。 但是,這里還有其他一些東西:

Cache-Control: private
Expires: Thu, 01 Jan 1970 00:00:00 GMT
ETag: "0.42.1-20181213080300000"
Cache-Control: max-age=60
...other headers

較低的緩存標頭是我的,而頂部的標頭是不需要的。 它們似乎來自org.apache.catalina.authenticator.AuthenticatorBase ,它似乎是所使用的嵌入式Tomcat的一部分。 我一直無法找到一種方法來訪問和修改此特定類的配置。

請告知如何擺脫不需要的標題。

我正在使用Spring Boot 1.5.18.RELEASE

為了更改Valve的配置,我必須使用EmbeddedServletContainerCustomizer在適當的生命周期階段從上下文中進行查找,如下所示。

@Component
public class TomcatCacheControlCustomizer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if(container instanceof TomcatEmbeddedServletContainerFactory){
            TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) container;
            containerFactory.addContextLifecycleListeners((LifecycleListener) event -> {
                Context context = (Context) event.getLifecycle();
                if(event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)){
                    if(context.getPipeline() != null){
                        Pipeline pipeline = context.getPipeline();
                        if(pipeline.getValves() != null){
                            Optional<Valve> authenticatorBase = Arrays.stream(pipeline.getValves()).filter(v -> v instanceof AuthenticatorBase).findFirst();
                            if(authenticatorBase.isPresent()){
                                ((AuthenticatorBase) authenticatorBase.get()).setDisableProxyCaching(false);
                            }
                        }
                    }
                }
            });
        }
    }

}

更新AuthenticatorBase的配置后,不需要的Cache-Control標頭不再添加到響應中,僅保留了我的自定義標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM