【问题标题】:Remove "Expires" HTTP header (for StreamedFiles)删除“过期”HTTP 标头(用于 StreamedFiles)
【发布时间】:2020-09-17 07:17:05
【问题描述】:

在 micronaut 过滤器中,我指定自己的标题,例如。 G。我使用“max-age”指令设置“Cache-Control”标头。因此,我想删除“Expires”标头,因为通过使用“Cache-Control”,“Expires”标头会被忽略 1

从过滤器返回 StreamedFile 时,“Expires”和“Date”标头由 FileTypeHandler 2 设置,我不知道如何更改。

有没有办法改变它?

例子:

@Filter("/**")
public class MyFilter implements HttpServerFilter {

    @Inject
    ImageService imageService;

    @Override
    public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
        File image = imageService.getImage(request);

        return Publishers.just(
                HttpResponse.ok(new StreamedFile(new FileInputStream(image), MediaType.IMAGE_JPEG_TYPE))
                        .header("Cache-Control", "max-age=31449600")
                        .header("Access-Control-Allow-Methods", "GET")
                        .header("Referrer-Policy",  "same-origin")
        );
    }

}

【问题讨论】:

    标签: java micronaut


    【解决方案1】:

    不确定为什么要从过滤器返回文件

    如果只是您确定的方法困扰您生成此标头,您可以覆盖它:

    @Singleton
    @Replaces(FileTypeHandler.class)
    public class CustomFileTypeHandler extends FileTypeHandler {
    
        public CustomFileTypeHandler(FileTypeHandlerConfiguration configuration) {
            super(configuration);
        }
    
        @Override
        protected void setDateAndCacheHeaders(MutableHttpResponse response, long lastModified) {
            //do nothing
        }
    }
    

    【讨论】:

    • 谢谢,这行得通。当然,我想控制器会更好。我发现我可以使用 "@Controller("{+path}")" 因为 "/**" 在控制器注释中不起作用。
    猜你喜欢
    • 2010-12-28
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2011-05-26
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多