我在浏览器的控制台中收到以下错误:
内容安全策略:页面的设置阻止了在
http://localhost:3000/favicon.ico(“default-src”)处加载资源。
我在网上搜索,发现这应该用下面的代码片段来解决:
<meta http-equiv="Content-Security-Policy" content="default-src *;
img-src * 'self' data: https: http:;
script-src 'self' 'unsafe-inline' 'unsafe-eval' *;
style-src 'self' 'unsafe-inline' *">
Run Code Online (Sandbox Code Playgroud)
我将此添加到我的前端app.component.html文件(我所有前端视图的父模板),但这并没有按预期工作。
我也因此尝试了多种排列无济于事。
我的前端在localhost:4200,后端在localhost:3000。
下面是来自我的后端服务器(中间件)的代码片段:
app.use(cors());
app.options('*',cors());
var allowCrossDomain = function(req,res,next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
app.use(allowCrossDomain);
Run Code Online (Sandbox Code Playgroud)
我现在还向我的后端 (Express) 服务器添加了以下中间件:
const csp = require('express-csp-header');
app.use(csp({
policies: {
'default-src': [csp.SELF, 'http://localhost:3000/', 'http://localhost:4200/' ],
'script-src': [csp.SELF, csp.INLINE],
'style-src': [csp.SELF],
'img-src': ['data:', 'favico.ico'],
'worker-src': [csp.NONE],
'block-all-mixed-content': true …Run Code Online (Sandbox Code Playgroud)