所以我在我的角度项目的index.html中添加了内容-安全策略。
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self';"
>
<title>Csptest</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <link rel="icon" type="image/x-icon" href="favicon.ico">-->
</head>
<body>
<app-root></app-root>
</body>
</html>
还清除了app.component.html中的所有内容。
app.component.html
<h2>App component works</h2>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'csptest';
}
但是当我运行这个项目时,它会被编译,但是当我转到localhost:4200时,它说
拒绝应用内联样式,因为它违反了以下内容安全策略指令:"default-src 'self'“。要么是‘不安全-内联’关键字,要么是散列('sha256-l5BdY8iHQaD9T7HFC/wOySvJobC7DMB33NJOSw+ToWw='),,要么是“不安全-.”)需要启用内联执行。还请注意,“style-src”未显式设置,因此“default-src”用作回退。
所以我在这里有点困惑,如果所有的东西都从app.component.html中删除了,并且项目中没有其他组件,那么为什么会出现这个错误。
发布于 2022-07-19 14:38:42
当错误消息声明
'unsafe-inline'
(对于style-src
没有问题,但对于script-src
或default-src
则不是)Content-Security-Policy: style-src 'unsafe-inline'
使用
Content-Security-Policy: style-src 'nonce-0123456789'
当使用nonce时,您必须在<style>
标记中引用它:
<style nonce="0123456789">
body { background: red; }
</style>
这种方法要求您在每次加载页面时生成一个新的new,并且可能更难实现。
有关详细信息,请参阅https://content-security-policy.com/examples/allow-inline-style/。
https://stackoverflow.com/questions/73038566
复制相似问题