我有一个带有以下 index.html 文件的 Angular 应用程序
考虑在我的 index.html 页面中,我有以下 SRI 代码(子资源完整性)
<html>
<head>
<meta http-equiv="Content-Security-Policy"
content="script-src 'self' scripts/alert.js 'unsafe-inline' 'unsafe-eval' 'sha256-qznLcsROx4GACP2dm0UCKCzCG+HiZ1guq6ZZDob/Tng='">
<script src="scripts/alert.js"
integrity="sha256-qznLcsROx4GACP2dm0UCKCzCG+HiZ1guq6ZZDob/Tng="
crossorigin="anonymous"></script>
</head>
</html>
如果我使用 require JS,那么我必须将包含“alert.js”的脚本移动到“main.js”文件,如下所示
require.config({
// alias libraries paths
paths: {
'jquery': '/scripts/alert'
},
// kick start application
deps: ['../app/require.bootstrap']
})
有人可以帮助我如何在 main.js 文件中包含完整性属性,同时在路径中引用 alert.js 脚本。
最佳答案
如果我正确理解您的问题,您希望对通过 require js 引用的脚本使用子资源完整性。请注意,为了做到这一点,您需要 RequireJS 2.1.19 或更高版本(参见 http://requirejs.org/docs/download.html)。
有关工作示例(引用 jQuery),请参阅此插件:http://plnkr.co/edit/kzqLjUThJRtoEruCCtMt?p=preview .希望您应该能够将此方法复制到您的项目中。
我的示例将完整性/跨域属性用于:
index.html
文件)main.js
和你感兴趣的东西)这是基于 RequireJS Hook
onNodeCreated
和像这样的代码onNodeCreated: function(node, config, module, path) {
node.setAttribute('integrity', integrityForModule);
node.setAttribute('crossorigin', 'anonymous');
}
请注意,此示例未将 SRI 用于配置文件
main.js
文件。为了实现这一点,要么index.html
中包含 RequireJS 配置内联页 main.js
(配置文件)通过一个额外的脚本标签(具有完整性/交叉),而不是通过 data-main
属性 关于angularjs - 使用 Require JS 的 angularJS 应用程序中的子资源完整性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34348536/