dav*_*hew 5 java firefox spring spring-mvc content-security-policy
我构建了一个简单的 Spring Boot Rest 控制器,它除了返回自定义 Java 对象 - 数据之外什么也不做。一切都编译并正常运行。当我从端点获取数据时,我得到了预期的数据。
\n然而,当在 Firefox 上使用“ Inspect Element ”查看底层时,我看到由于内容安全策略 (CSP)导致的错误。内容安全策略错误如下:
\n\n“内容安全策略:页面\xe2\x80\x99s 设置阻止加载 http://localhost:8081/favicon.ico (\xe2\x80\x9cdefault-src\xe2\x80\x9d) 上的资源。”
\n我尝试了几种解决方案,但均无济于事。
\n这是我的文件。
\n应用程序属性 = application.properties
\nspring.mvc.favicon.enabled=false\n\nRun Code Online (Sandbox Code Playgroud)\n主文件 - DemoApplication
\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n\n@SpringBootApplication\npublic class DemoApplication {\n\n public static void main(String[] args) {\n System.getProperties().put( "server.port", 8081);\n SpringApplication.run(DemoApplication.class, args);\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n休息控制器=数据控制器
\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\npublic class DataController\n{\n\n @GetMapping("/data")\n public Data data()\n {\n \n return new Data(123, "abc");\n \n }\n \n}\nRun Code Online (Sandbox Code Playgroud)\n返回类型 = 数据
\npublic class Data\n{\n\n private final long id;\n private final String data;\n\n public Data(long id, String data) { this.data = data; }\n\n public long getId() { return this.id; }\n public String getData() { return this.data; }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n
经过一番调试后,我发现该问题似乎是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";
}
}
Run Code Online (Sandbox Code Playgroud)
我运行了比这更多的示例,但这演示了 String 作为响应类型如何在 Firefox 上不会引发错误,但 JSON 作为响应类型会引发错误。
在其他浏览器上尝试这两个端点时,JSON 或 String 似乎都没有错误。
然后我意识到 - 与其他浏览器不同,Firefox 有一个内置的JSON Viewer。这意味着,如果整个页面接收到的数据纯粹是 JSON,那么 Firefox 将以完全不同的方式呈现该页面。Firefox 将使用一些奇特的 UI 来组织 JSON,以便于阅读/解析,而不是仅仅将纯 JSON 字符串吐出到页面上。
因此,作为最终测试,我关闭了 JSON Viewer,然后重新加载页面 - 一切都按预期工作。
我还要补充一点 -我真的不认为这是 Firefox 的一个错误- @granty 指出Mozilla 自己正在调查这个问题。这只发生在整个响应都是 JSON 的端点上。
如果从大局来看,返回纯 JSON 的端点实际上只是数据流,而不是由普通用户使用。那么你真的需要一个图标吗?哈哈,可能不是。
| 归档时间: |
|
| 查看次数: |
1675 次 |
| 最近记录: |