如何在 express.js node.js 中设置 X-Frame-Options

How to set X-Frame-Options in express.js node.js

我想在多个桌面/移动网络客户端的 iframe 中提供一些静态资产。

现在,我如何将允许的一组特定来源列入白名单 设置 X-Frame-Options headers 以便资源可以作为 iframe 嵌入 在不同的桌面/移动网络客户端中。 对于所有其他来源,拒绝访问此资源。

经过一点挖掘,我开始了 -

const app = express();

var allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Authorization');
    if (req.method === "OPTIONS") res.send(200);
    else next();
}
app.use(allowCrossDomain);

现在在这里我如何设置 X-Frame-Options header 与白名单原始值在这里 -

你应该导入 helmet and use frameguard to get some origins whitelisted. More on this topic: MDN X-FRAME-OPTIONS Best Practice Security

你只需要 helmet

npm install helmet --save 

const express = require('express')
const helmet = require('helmet')

const app = express()

app.use(helmet.frameguard())