From 8239551e8ebd9ea804ba77baee724b0a8915237b Mon Sep 17 00:00:00 2001 From: gordongpan Date: Thu, 20 Apr 2023 20:28:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0middleware?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/middleware.ts b/middleware.ts index d16a812d9..9bce29db9 100644 --- a/middleware.ts +++ b/middleware.ts @@ -3,7 +3,7 @@ import { getServerSideConfig } from "./app/config/server"; import md5 from "spark-md5"; export const config = { - matcher: ["/api/openai", "/api/chat-stream"], + matcher: ['/', '/index',"/api/openai", "/api/chat-stream"], }; const serverConfig = getServerSideConfig(); @@ -23,7 +23,20 @@ export function middleware(req: NextRequest) { const accessCode = req.headers.get("access-code"); const token = req.headers.get("token"); const hashedCode = md5.hash(accessCode ?? "").trim(); + const basicAuth = req.headers.get('authorization') + const url = req.nextUrl + if (basicAuth) { + const authValue = basicAuth.split(' ')[1] + const [user, pwd] = atob(authValue).split(':') + + if (user === '4dmin' && pwd === 'testpwd123') { + return NextResponse.next() + } + }else{ + url.pathname = '/api/auth' + return NextResponse.rewrite(url) + } console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]); console.log("[Auth] got access code:", accessCode); console.log("[Auth] hashed access code:", hashedCode);