当前位置: 首页 > news >正文

解决Content Security Policy (CSP)问题

在这里插入图片描述

在这里插入图片描述
做的一个简单的自动识别图片文字是否一致的程序 引用的百度的OCR 在公司的代码运行正常 在家运行报错了

Refused to apply inline style because it violates the following Content Security Policy directive: “default-src ‘none’”. Either the ‘unsafe-inline’ keyword, a hash (‘sha256-QIjW/+aUzfg58HcITJNHkkCTGmLovNUIQbL+Zq2TsIE=’), or a nonce (‘nonce-…’) is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the ‘unsafe-hashes’ keyword is present. Note also that ‘style-src’ was not explicitly set, so ‘default-src’ is used as a fallback. g @ customElements.js:1 customElements.js:1 Refused to apply inline style because it violates the following Content Security Policy directive: “default-src ‘none’”. Either the ‘unsafe-inline’ keyword, a hash (‘sha256-QIjW/+aUzfg58HcITJNHkkCTGmLovNUIQbL+Zq2TsIE=’), or a nonce (‘nonce-…’) is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the ‘unsafe-hashes’ keyword is present. Note also that ‘style-src’ was not explicitly set, so ‘default-src’ is used as a fallback. g @ customElements.js:1 prepare.js:1 Refused to execute inline script because it violates the following Content Security Policy directive: “default-src ‘none’”. Either the ‘unsafe-inline’ keyword, a hash (‘sha256-3bzWVxQE32IZQKH9eh8KzyHuhXOlMrboDVVBRd0fWTU=’), or a nonce (‘nonce-…’) is required to enable inline execution. Note also that ‘script-src’ was not explicitly set, so ‘default-src’ is used as a fallback. c @ prepare.js:1 (索引):1 Failed to load resource: the server responded with a status of 404 (Not Found)

Content Security Policy (CSP) 的问题把 inline style 和 inline script 全部拦掉了

解决方法
方法 1:关闭 CSP

用 VS Code 的 Live Server 插件或 http-server 跑,不要用直接打开 file://。
再或者在浏览器里启动的时候加上参数关闭 CSP,比如 Chrome:chrome.exe --disable-web-security --user-data-dir=c:\temp\chrome

方法 2:修改 CSP 规则

如果你控制的是自己服务器,可以在响应头里加:
Content-Security-Policy: default-src * ‘unsafe-inline’ ‘unsafe-eval’ data: blob:
这样就允许内联的

方法 3:修改你现在的代码

你现在的代码里有:

这样就不会触发 inline 的 CSP 限制。

http://www.xdnf.cn/news/1425403.html

相关文章:

  • Java类加载机制
  • 软件使用教程(四):Jupyter Notebook 终极使用指南
  • 【iOS】关键字复习
  • javaScript变量命名规则
  • MySQL基础知识保姆级教程(四)视图与约束
  • Chrome 如何清除浏览器缓存
  • 【开题答辩全过程】以 基于SpringBoot的校园一卡通管理系统的设计与实现为例,包含答辩的问题和答案
  • ESP32驱动数字麦克风INMP441
  • A2A + MCP 的python实现的最小可运行骨架
  • Jmeter实现参数化的4种方式
  • 构建AI智能体:二十、妙笔生花:Gradio集成DashScope Qwen-Image模型实现文生图
  • 人脸识别备案的重要意义
  • ES6新特性:JavaScript的进化装备箱[特殊字符]
  • 记一次使用函数式接口
  • A股大盘数据-20250901 分析
  • GD32入门到实战25--独立看门狗
  • JAVA后端开发——MyBatis 结合 MySQL JSON 类型查询详解
  • 【STM32】贪吃蛇 [阶段 3] 增强模块结构(架构优化)
  • curl 介绍及使用教程
  • python爬虫之selenium库进阶(小白五分钟从入门到精通)
  • 基本渗透概念
  • Raft 协议在 Nacos 中的实现
  • 从零开始实现Shell | Linux进程调度实战
  • Product Hunt 每日热榜 | 2025-09-01
  • 基于YOLOv11的脑卒中目标检测及其完整数据集——推动智能医疗发展的新机遇!
  • 齿轮里的 “双胞胎”:分度圆与节圆
  • [React]监听Form中某个字段的变化
  • 微算法科技(NASDAQ:MLGO)张量网络与机器学习融合,MPS分类器助力顶夸克信号识别
  • deepseek doubao chatgpt 优缺点分析
  • 并发--并发中的线程状态及不同状态下线程所在队列