This commit is contained in:
2025-02-19 00:34:51 +08:00
parent cf1cc83547
commit 2f3ffe2a34
33 changed files with 3180 additions and 832 deletions

View File

@@ -10,6 +10,7 @@ module.exports = {
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
) {
@@ -19,8 +20,8 @@ module.exports = {
jwt.verify(req.headers.token,config.jwt,function(err,decode){
if(err){
res.json({
status: 501,
message: '身份凭证失效或不存在,请登录'
status: 403,
message: `身份凭证失效或不存在,请登录${err.message}`
})
}else{
next();
@@ -28,26 +29,21 @@ module.exports = {
});
} else { // 无token
res.json({
status: 500,
message: '缺少token参数请检查'
status: 403,
message: '无权限访问接口,您需要先登录才能使用!'
})
}
}
} catch (e) {
console.log("--------");
console.log("e --------: ", e);
console.log("--------");
res.json({
status: 500,
message: '抱歉,公钥加密有误,请检查公钥'
message: `抱歉,私钥解密出错!错误信息:${e.message}`
})
}
} else {
res.json({
status: 500,
message: '请求头缺少data参数'
message: '请求头缺少data字段'
})
}
}

View File

@@ -1,53 +0,0 @@
const NodeRsa = require("node-rsa");
const fs = require("fs");
const path = require("path");
const config = require("../config");
class Rsa {
constructor() {
this.private_key = this.readFiles(this.getFilePath(config.rsa.private_key))
this.public_key = this.readFiles(this.getFilePath(config.rsa.public_key))
}
// 公钥加密
PublicKeyEncryption(text) {
return (new NodeRsa(this.public_key)).encrypt(JSON.stringify(text), 'base64')
}
// 公钥解密
PublicKeyDecryption(text) {
return JSON.parse((new NodeRsa(this.public_key)).decryptPublic(text, 'utf8'))
}
// 私钥加密
PrivateKeyEncryption(text) {
return (new NodeRsa(this.private_key)).encryptPrivate(JSON.stringify(text), 'base64')
}
// 私钥解密
PrivateKeyDecryption(text) {
return JSON.parse((new NodeRsa(this.private_key)).decrypt(text, 'utf8'))
}
/**
* 获取path.join文件地址
* @param paths
* @returns {string}
*/
getFilePath(paths) {
return path.join(__dirname, paths)
}
/**
* 读取文件数据
* @param path
* @returns {*}
*/
readFiles(path) {
return fs.readFileSync(path).toString()
}
}
module.exports = new Rsa();

View File

@@ -19,21 +19,21 @@ class Rsa {
}
// 公钥加密
PublicKeyEncryption(text) {
return (new NodeRsa(this.public_key)).encrypt(JSON.stringify(text), 'base64')
}
// // 公钥加密
// PublicKeyEncryption(text) {
// return (new NodeRsa(this.public_key)).encrypt(JSON.stringify(text), 'base64')
// }
// 公钥解密
PublicKeyDecryption(text) {
return JSON.parse((new NodeRsa(this.public_key)).decryptPublic(text, 'utf8'))
}
// // 公钥解密
// PublicKeyDecryption(text) {
// return JSON.parse((new NodeRsa(this.public_key)).decryptPublic(text, 'utf8'))
// }
// 私钥加密
PrivateKeyEncryption(text) {
return (new NodeRsa(this.private_key)).encryptPrivate(JSON.stringify(text), 'base64')
}
// // 私钥加密
// PrivateKeyEncryption(text) {
// return (new NodeRsa(this.private_key)).encryptPrivate(JSON.stringify(text), 'base64')
// }
// 私钥解密
PrivateKeyDecryption(text) {