【问题标题】:How to set no-store in om.vaadin.server.StreamResource如何在 om.vaadin.server.StreamResource 中设置 no-store
【发布时间】:2021-10-04 21:22:26
【问题描述】:

我有一个 vaadin 7 应用程序来下载 csv 文件。当我在 csv 文件的响应头中设置 streamResource.setCacheTime(0); Cache-Control: no-cache set 时。 但是如何在资源的响应标头中也设置 no-store。我只想停止在浏览器中保留我的 csv 文件。所以攻击者不能使用它。 以下方法无效streamResource.getStream().setParameter("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");

这个也没有

response.setHeader("Cache-Control",  "no-cache, no-store, max-age=0, must-revalidate");

请帮忙

【问题讨论】:

    标签: web-applications httpresponse java-11 vaadin7 websecurity


    【解决方案1】:

    streamResource.getStream().setParameter(...) 不起作用,因为 getStream() 每次调用时都会创建一个新实例。

    您可以做的是创建StreamResource 的自定义子类,它会覆盖getStream(),以便在返回原始流之前对其进行进一步更改,例如:

    public class NoStoreStreamResource extends StreamResource {
      public NoStoreStreamResource(StreamSource streamSource, String filename) {
        super(streamSource, filename);
      }
      
      @Override
      public DownloadStream getStream() {
        DownloadStream ds = super.getStream();
        ds.setParameter("Cache-Control",  "no-cache, no-store, max-age=0, must-revalidate");
        return ds;
      }
    }
    

    【讨论】:

    • 这个答案是正确的,但不适用于我的情况。有没有替代方法?类似于应用过滤器?
    • 根据具体情况,可能会覆盖适当的handleConnectorRequest 方法(在不同情况下使用其中三个)以应用标题。下一步是添加一个 Vaadin RequestHandler 来识别特定请求并将标头值注入响应中,或者用自定义版本替换适当的现有值。然后还有可能是 Servlet Filter 做类似的事情,或前端服务器中的处理规则(例如 RewriteRule 与 Apache httpd)。
    猜你喜欢
    • 2012-11-03
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2019-11-05
    • 1970-01-01
    相关资源
    最近更新 更多