我構建了一個簡單的 Spring Boot Rest Controller,它只回傳一個自定義的 Java 物件 - 資料。一切正常編譯和運行。當我從端點獲取資料時,我會按預期獲得資料。
但是,當在 Firefox 上使用“ Inspect Element ”查看引擎蓋時,我看到由于
“內容安全策略:頁面的設定阻止了在 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; }
}
uj5u.com熱心網友回復:
經過一些除錯,我發現問題似乎是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 似乎都沒有錯誤。
And then I realized - unlike the other browsers, Firefox has a built-in 
I would also add - I don't really think this is a bug on Firefox's part -- @granty pointed out that Mozilla themselves are looking into this. This only happens for endpoints where the entirety of the response is JSON.
If you look at it big picture, endpoints that return purely JSON are really just meant to be streams of data, and not consumed by the average user. So do you really need an icon to be included? lol, probably not.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/356505.html
上一篇:如何在SwiftUI串列中設定NavigationLink
下一篇:何時使用Microsoft.Data.SqlClient.SqlException與System.Data.SqlClient.SqlException?
