添加token验证规则

防止某些不怀好意的入调用
This commit is contained in:
Jerry 2025-04-29 13:25:18 +08:00
parent 5e1911a2c3
commit c96d53dc44
3 changed files with 15 additions and 3 deletions

1
.env
View File

@ -4,3 +4,4 @@ RD_PORT=6379
RD_ADD=127.0.0.1 RD_ADD=127.0.0.1
WS_SECRET=114514 WS_SECRET=114514
WS_PORT=4001 WS_PORT=4001
TOKEN=54188

View File

@ -1,6 +1,7 @@
import express from 'express'; import express from 'express';
import response from '../../utils/core/response'; import response from '../../utils/core/response';
import BotService from './bot.service'; import BotService from './bot.service';
import Config from '../../utils/core/config';
class BotController { class BotController {
private readonly router: express.Router; private readonly router: express.Router;
@ -20,8 +21,18 @@ class BotController {
private postBotsId = async (req: express.Request, res: express.Response): Promise<void> => { private postBotsId = async (req: express.Request, res: express.Response): Promise<void> => {
try { try {
const result = await BotService.getBotId(); const token = req.body.token;
await response.success(res, result); if (token.toString() === Config.get('TOKEN').toString()) {
const result = await BotService.getBotId();
await response.success(res, result);
} else {
await response.error(
res,
'token验证失败..',
404,
`有个小可爱使用了错误的token:${JSON.stringify(token)}`
);
}
} catch (err) { } catch (err) {
await response.error(res, `请求失败..`, 500, err); await response.error(res, `请求失败..`, 500, err);
} }

View File

@ -48,7 +48,7 @@ async function testGetAPI() {
async function testPostAPI() { async function testPostAPI() {
try { try {
const response = await axios.post('http://localhost:4000/api/bot/getBotId', {}); const response = await axios.post('http://localhost:4000/api/bot/getBotId', { token: 54188 });
console.log('[HTTP][POST] Response:', response.data); console.log('[HTTP][POST] Response:', response.data);
} catch (err) { } catch (err) {
console.error('[HTTP][POST] Error:', err); console.error('[HTTP][POST] Error:', err);