【问题标题】:favicon.ico blocked by CSP when fetching JSON on Firefox在 Firefox 上获取 JSON 时,CSP 阻止了 favicon.ico
【发布时间】:2021-12-17 12:18:21
【问题描述】:

我已经构建了一个简单的 Spring Boot Rest 控制器,它只返回一个自定义 Java 对象 - 数据。一切都编译并正常运行。当我从端点获取数据时,我得到了预期的数据。

但是,在 Firefox 上使用“Inspect Element”查看引擎盖时,我看到了一个由于Content Security Policy (CSP) 而导致的错误。 Content-Security-Policy 错误说明如下:

“内容安全策略:该页面的设置阻止了在 http://localhost:8081/favicon.ico (“default-src”) 处加载资源。”

我尝试了一些解决方案,都无济于事。

  • 我尝试通过 application.properties 禁用图标,但这似乎没有任何效果。
  • 我创建了一个名为“favicon.ico”的图标并将其放置在正确的目录中。很烦人,这个页面还是报错,而我的其他页面都开始出现图标了。
  • 我尝试了 许多 标题排列,包括将 Content-Security-Policy 标题设置为默认 src self。没有工作,尽管这可能是问题的根源,因为似乎有很多我没有完全掌握的活动部件。
  • 我尝试为“/favicon.ico”创建一个 GET 端点,但这似乎根本没有完成任何事情。
    • 此时我已将图标添加到我的目录中,因此当我尝试点击端点时,它只是向我发送了我的图标图像,该图标也显示在浏览器顶部的选项卡中,并且日志中没有错误。
  • 我试图弄乱 WebSecurityConfigurerAdapter,但很快就失控了,坦率地说,其中很多都没有意义。

这是我的文件。

应用程序属性 = application.properties

spring.mvc.favicon.enabled=false

主文件 - DemoApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      System.getProperties().put( "server.port", 8081);
      SpringApplication.run(DemoApplication.class, args);
   }

}

休息控制器 = 数据控制器

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DataController
{

   @GetMapping("/data")
   public Data data()
   {
   
      return new Data(123, "abc");
   
   }
  
}

返回类型 = 数据

public class Data
{

    private final long id;
    private final String data;

    public Data(long id, String data) { this.data = data; }

    public long getId() { return this.id; }
    public String getData() { return this.data; }

}

【问题讨论】:

    标签: java spring spring-mvc firefox content-security-policy


    【解决方案1】:

    经过一些调试,我发现问题似乎是Firefox 特有的,并且只针对返回 JSON 对象的端点

    例如,如果我构建了一个仅返回字符串的端点,Firefox 将返回该字符串,并且图标将位于顶部的选项卡中。

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class DataController
    {
    
       @GetMapping("/data")
       public Data data()
       {
       
          return new Data(123, "abc");
       
       }
      
       @GetMapping("/abc123")
       public String abc123()
       {
       
          return "abc123";
       
       }
       
    }
    

    我运行了比这更多的示例,但这演示了 String 作为响应类型如何在 Firefox 上不会引发错误,但 JSON 作为响应类型会引发错误。

    在其他浏览器上尝试这两个端点时,似乎 JSON 或 String 都没有错误。

    然后我意识到 - 与其他浏览器不同,Firefox 有一个内置的 JSON Viewer。这意味着,如果为整个页面接收到的数据纯粹是 JSON,那么 Firefox 将呈现完全不同的页面。 Firefox 将使用一些精美的 UI 来组织 JSON,而不是仅仅将纯 JSON 字符串吐出到页面上,以便于阅读/解析。

    所以,作为最后的测试,我turned off the JSON Viewer,然后重新加载了页面——一切都按预期工作。

    我还要补充 - 我真的不认为这是 Firefox 的错误 - @granty 指出 Mozilla themselves are looking into this。这仅发生在 整个响应为 JSON 的端点。

    如果你从大局来看,返回纯 JSON 的端点实际上只是数据流,而不是普通用户使用的。那么你真的需要一个图标来包含吗?大声笑,可能不会。

    【讨论】:

    【解决方案2】:

    您可以在解决方案中添加jquery.unobtrusive-ajax.js 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多