ProxyReqOptDecorator rewrite the request header as follows:
var express = require('express');
var proxy = require('express-http-proxy')
var app = express();
app.all(The '*'.function (req, res, next) {
res.header("Access-Control-Allow-Origin", req.headers.origin);
res.header("Access-Control-Allow-Methods"."PUT,POST,GET,DELETE,OPTIONS");
res.header("Access-Control-Allow-Credentials"."true");
res.header("Content-Type"."application/json; charset=utf-8");
if (req.method == 'OPTIONS') {
res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
res.send(200); /* Make the options request return quickly */
}
else{ next(); }});// Code forward
app.use('/ProxySSO', proxy(req= > {
return req.headers.apiurl
}, {
filter: function (req, res) {
// User name/system id/user permission
let { systemName, masterName, powers } = req.tokenDecode;
const { originalUrl } = req;
const url = originalUrl.replace('/ProxySSO'.' ').split('? ') [0];
/** * Interface permission check *@param {String} Url Interface address *@param {Object} Powers RBAC permission list *@returns {boolean} Whether you have permission */
const checkPowers = (url, powers) = > {
// do something
}
const hasPowers = checkPowers(url, powers);
// Check permissions
return hasPowers;
},
proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
// add masterName to masterName
// proxyReqOpts.headers['masterName'] = srcReq.tokenDecode.masterName;
returnproxyReqOpts; }}));module.exports = app;
Copy the code