51 lines
1.8 KiB
JavaScript
51 lines
1.8 KiB
JavaScript
const config = require("../config")
|
||
const rsa = require("../utils/rsa")
|
||
const jwt = require('jsonwebtoken');
|
||
/**
|
||
* 拦截器,验证 token,data
|
||
* @type {{Validator(*, *, *): void}}
|
||
*/
|
||
module.exports = {
|
||
Validator(req, res, next) {
|
||
if(req.headers.hasOwnProperty("data")) {
|
||
try {
|
||
req.headers.data = rsa.PrivateKeyDecryption(req.headers.data)
|
||
// console.log("req.headers.data: ", req.headers.data);
|
||
if(req.url == config.filter.login || req.url == config.filter.add
|
||
|| req.url == config.filter.home
|
||
) {
|
||
next()
|
||
} else {
|
||
if(req.headers.hasOwnProperty("token")) {
|
||
jwt.verify(req.headers.token,config.jwt,function(err,decode){
|
||
if(err){
|
||
res.json({
|
||
status: 403,
|
||
message: `身份凭证失效或不存在,请登录!${err.message}`
|
||
})
|
||
}else{
|
||
next();
|
||
}
|
||
});
|
||
} else { // 无token
|
||
res.json({
|
||
status: 403,
|
||
message: '无权限访问接口,您需要先登录才能使用!'
|
||
})
|
||
}
|
||
}
|
||
} catch (e) {
|
||
res.json({
|
||
status: 500,
|
||
message: `抱歉,私钥解密出错!错误信息:${e.message}`
|
||
})
|
||
}
|
||
} else {
|
||
res.json({
|
||
status: 500,
|
||
message: '请求头中缺少data字段'
|
||
})
|
||
}
|
||
}
|
||
}
|