使用__webpack_nonce__设置CSP的方法

参考来源:https://stackoverflow.com/que…
webpack配置,将index.js变为index.ejs模板
const HtmlWebpackPlugin = require(‘html-webpack-plugin’)

module.exports = {
entry: ‘index.js’,
output: {
path: __dirname + ‘/dist’,
filename: ‘index_bundle.js’
},
plugins: [
new HtmlWebpackPlugin({
filename: __dirname + ‘/dist/index.ejs’,
})
]
}
webpack的入口文件(index.js)添加
__webpack_nonce__ = ‘<%=nonce%>’;
服务端程序,遇到index.js的请求,就替换掉<%=nonce%>

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理