express中间件中怎么获取req.headers.Authorization? 为什么我一直是undefined。
下面代码设置了也不起作用。各位大哥指点我一下,谢谢!
var allowCors = function (req,res,next){
res.header("Access-Control-Expose-Headers", "Authorization");
next();
}
app.use(allowCors);
// 验证授权中间件
const verifyAuth = async (req,res,next) => {
console.log('验证授权中间件');
let authorization = req.headers['Authorization']
console.log(authorization); // 这里一直是undefined
try{
// 获取token
let token = authorization.replace('Bearer ','')
// 验证token
let result = jwt.verify(token,config.PUBLIC_KEY,{
algorithms:['RS256']
})
req.user = result
await next()
} catch (e) {
await next(new Error(errorTypes.INVALID_TOKEN))
}
}
前端要在请求头加上
Authorization
,在axios
的请求拦截器中加上config.headers['Authorization'] = 'xxx'