Access-Control-Allow-Origin Multiple Origin Domains?

前端 未结 30 2058
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 07:08

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header?

I\'m aware of the *, but it is too open. I rea

30条回答
  •  无人共我
    2020-11-21 07:38

    For ExpressJS applications you can use:

    app.use((req, res, next) => {
        const corsWhitelist = [
            'https://domain1.example',
            'https://domain2.example',
            'https://domain3.example'
        ];
        if (corsWhitelist.indexOf(req.headers.origin) !== -1) {
            res.header('Access-Control-Allow-Origin', req.headers.origin);
            res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
        }
    
        next();
    });
    

提交回复
热议问题