feat:后台增加密钥
This commit is contained in:
parent
b6d3a5a3a8
commit
017e26c686
13
openRenamerBackend/api/PublicApi.ts
Normal file
13
openRenamerBackend/api/PublicApi.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Context } from "koa";
|
||||
import config from "../config";
|
||||
|
||||
const router = {};
|
||||
|
||||
/**
|
||||
* 判断token是否正确
|
||||
*/
|
||||
router["POST /public/checkToken"] = async function (ctx: Context) {
|
||||
ctx.body = ctx.request.body.token === config.token;
|
||||
};
|
||||
|
||||
export default router;
|
@ -6,6 +6,7 @@ const rootPath = path.resolve(__dirname, '..');
|
||||
let config = {
|
||||
rootPath,
|
||||
port: process.env.PORT ? parseInt(process.env.PORT) : 8089,
|
||||
token: process.env.TOKEN ? process.env.TOKEN : null,
|
||||
urlPrefix: '/openRenamer/api',
|
||||
//是否为windows平台
|
||||
isWindows: process.platform.toLocaleLowerCase().includes("win"),
|
||||
@ -18,7 +19,8 @@ let config = {
|
||||
keepExtenstions: true,
|
||||
maxFieldsSize: 1024 * 1024
|
||||
}
|
||||
}
|
||||
},
|
||||
publicPath: new Set(["POST/public/checkToken"])
|
||||
};
|
||||
|
||||
export default config;
|
||||
|
@ -1,8 +1,15 @@
|
||||
import log from '../util/LogUtil';
|
||||
import config from "../config";
|
||||
|
||||
let f = async (ctx, next) => {
|
||||
try {
|
||||
await next();
|
||||
//检查是否有密码
|
||||
if (checkToken(ctx)) {
|
||||
await next();
|
||||
} else {
|
||||
ctx.status = 401;
|
||||
ctx.body = "密钥验证错误";
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error.status != undefined) {
|
||||
ctx.status = error.status;
|
||||
@ -14,4 +21,16 @@ let f = async (ctx, next) => {
|
||||
}
|
||||
}
|
||||
|
||||
function checkToken(ctx) {
|
||||
if (!config.token) {
|
||||
return true;
|
||||
}
|
||||
let requestPath = ctx.method + ctx.path.replace(config.urlPrefix, "");
|
||||
if (config.publicPath.has(requestPath)) {
|
||||
return true;
|
||||
}
|
||||
return config.token == ctx.headers.token;
|
||||
|
||||
}
|
||||
|
||||
export default f;
|
1886
openRenamerBackend/package-lock.json
generated
Normal file
1886
openRenamerBackend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user