Refused to load the script xxxxxx because it violates the following Content Security Policy directive:"script-src 'self' xxxxxxxxxxxxx" 瀏覽器安全問題


今天在公司碰到一個這樣的問題:大致問題是這樣的,我把新生成的國外cdn路徑放到頁面上來用來查看,發現會出現如下類似的錯誤信息

Refused to load the script xxxxxx because it violates the following Content Security Policy directive:"script-src 'self' xxxxxxxxxxxxx"

原圖已經沒了,我在阮一峰的博客上把這張圖貼上來,這個問題主要是由於瀏覽器為了防止跨域腳本攻擊,而推出"網頁安全政策"(Content Security Policy,縮寫 CSP),

而我的nginx服務器開啟了CSP,所以在對另一個域名下的資源就不能啟動js腳本。

CSP的含義如下:

#激活內容安全策略Content Security Policy (CSP) ,大部分瀏覽器支持
# 告訴瀏覽器只能從本域名和你顯式指定的網址下載腳本。

接着就是修改nginx配置,了解以下概念,我找到原先unsafe-inline 指向的cdn域名,利用通配符*使得該所有二級域名都符合unsafe-inline 

最后訪問成功!

 總結:首先你已經把資源文件放入到服務器上,配置好了nginx,能正常運行,這時你想對瀏覽器做一個限制,想弄的安全一點,不想讓外人對你的網站注入病毒腳本,所以你在nginx配置上添加 add_header  Content-Security-Policy ,把你需要訪問的url寫進去 ,這樣就  算完成CSP了。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 Electron中Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Note that 'script-src' was not explicitly set, so 'default-s 解決 Jenkins 中無法展示 HTML 樣式的問題,csp問題,Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'style-src' was not explicitly set, so 'default-src' is used as a fallback.錯誤應該怎么解決? iframe because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self' 開發chrome插件報錯:Refused to execute inline event handler because it violates the following Content Security Policy directive vscode electron error (Refused to execute inline event handler because it violates the following Content Security Policy directive ... 和 Uncaught ReferenceError: process is not defined) it violates the following Content Security Policy directive: "default-src 'self' http://example.com". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback. Refused to display because an ancestor violates the following Content Security問題簡單解決 解決 iframe Content Security Policy directive 錯誤
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM