英文:
Remove "Expires" HTTP header (for StreamedFiles)
问题
在 micronaut 过滤器中,我可以指定自己的头部,例如,我使用 "Cache-Control" 头部并设置 "max-age" 指令。因此,我想要移除 "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")
);
}
}
英文:
In a micronaut filter I specify my own headers, e. g. I set the "Cache-Control" header with a "max-age" directive. Therefore I want to remove the "Expires" header because by using "Cache-Control" the "Expires" header is ignored 1.
When returning a StreamedFile from a filter the "Expires" and "Date" header is set by FileTypeHandler 2 and I do not know how to change this.
Are there options to change this?
Example:
@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")
);
}
}
答案1
得分: 2
不确定您为什么要从过滤器中返回文件。
如果只是您识别出的方法困扰您生成这些标头,您可以直接覆盖它:
@Singleton
@Replaces(FileTypeHandler.class)
public class CustomFileTypeHandler extends FileTypeHandler {
public CustomFileTypeHandler(FileTypeHandlerConfiguration configuration) {
super(configuration);
}
@Override
protected void setDateAndCacheHeaders(MutableHttpResponse response, long lastModified) {
// 什么也不做
}
}
英文:
Not sure why exactly you want to return a file from a filter
If it is just the method you identified that bother you generating this headers, you can just override it :
@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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论