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