feat: 添加环境变量配置并优化部署路径
添加开发和生产环境变量文件 更新前端构建命令以使用不同模式 优化部署脚本中的前端部署路径 在认证守卫中添加日志记录
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -16,3 +16,6 @@ dist-electron
|
|||||||
unpackage
|
unpackage
|
||||||
.cursor
|
.cursor
|
||||||
qingyun
|
qingyun
|
||||||
|
plan
|
||||||
|
.trae
|
||||||
|
plans
|
||||||
@@ -1,2 +1,5 @@
|
|||||||
# ARK API Key (用于 AI 推荐)
|
# ARK API Key (用于 AI 推荐)
|
||||||
VITE_ARK_API_KEY=a63d58b6-cf56-434b-8a42-5c781ba0822a
|
VITE_ARK_API_KEY=a63d58b6-cf56-434b-8a42-5c781ba0822a
|
||||||
|
|
||||||
|
# 后端 API 地址
|
||||||
|
VITE_API_BASE_URL=http://localhost:3000/
|
||||||
5
frontend/.env.production
Normal file
5
frontend/.env.production
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# ARK API Key (用于 AI 推荐)
|
||||||
|
VITE_ARK_API_KEY=a63d58b6-cf56-434b-8a42-5c781ba0822a
|
||||||
|
|
||||||
|
# 后端 API 地址
|
||||||
|
VITE_API_BASE_URL=http://139.180.190.142:3000/
|
||||||
@@ -5,8 +5,8 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "vue-tsc -b && vite build",
|
"build": "vue-tsc -b && vite build --mode production",
|
||||||
"build:watch": "concurrently \"vue-tsc -b --watch\" \"vite build --watch\"",
|
"build:watch": "concurrently \"vue-tsc -b --watch\" \"vite build --watch --mode development\"",
|
||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {
|
|||||||
ExecutionContext,
|
ExecutionContext,
|
||||||
Injectable,
|
Injectable,
|
||||||
UnauthorizedException,
|
UnauthorizedException,
|
||||||
|
Logger,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { Request } from 'express';
|
import { Request } from 'express';
|
||||||
@@ -10,6 +11,8 @@ import { UsersService } from '../../users/users.service';
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthGuard implements CanActivate {
|
export class AuthGuard implements CanActivate {
|
||||||
|
private readonly logger = new Logger(AuthGuard.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private configService: ConfigService,
|
private configService: ConfigService,
|
||||||
private usersService: UsersService,
|
private usersService: UsersService,
|
||||||
@@ -22,6 +25,8 @@ export class AuthGuard implements CanActivate {
|
|||||||
const enableBasicAuth =
|
const enableBasicAuth =
|
||||||
this.configService.get<string>('ENABLE_BASIC_AUTH') === 'true';
|
this.configService.get<string>('ENABLE_BASIC_AUTH') === 'true';
|
||||||
|
|
||||||
|
this.logger.log(`Basic Auth enabled: ${enableBasicAuth}`);
|
||||||
|
|
||||||
if (!enableBasicAuth) {
|
if (!enableBasicAuth) {
|
||||||
// 如果未启用 Basic Auth,允许所有访问
|
// 如果未启用 Basic Auth,允许所有访问
|
||||||
return true;
|
return true;
|
||||||
@@ -31,6 +36,7 @@ export class AuthGuard implements CanActivate {
|
|||||||
const authHeader = request.headers['authorization'] as string;
|
const authHeader = request.headers['authorization'] as string;
|
||||||
|
|
||||||
if (!authHeader || !authHeader.startsWith('Basic ')) {
|
if (!authHeader || !authHeader.startsWith('Basic ')) {
|
||||||
|
this.logger.warn('Missing or invalid Authorization header');
|
||||||
throw new UnauthorizedException('Missing or invalid Authorization header');
|
throw new UnauthorizedException('Missing or invalid Authorization header');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,16 +48,22 @@ export class AuthGuard implements CanActivate {
|
|||||||
const [username, password] = credentials.split(':');
|
const [username, password] = credentials.split(':');
|
||||||
|
|
||||||
if (!username || !password) {
|
if (!username || !password) {
|
||||||
|
this.logger.warn('Invalid credentials format');
|
||||||
throw new UnauthorizedException('Invalid credentials format');
|
throw new UnauthorizedException('Invalid credentials format');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.log(`Attempting login for user: ${username}`);
|
||||||
|
|
||||||
// 验证用户
|
// 验证用户
|
||||||
const user = await this.usersService.validateUser(username, password);
|
const user = await this.usersService.validateUser(username, password);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
this.logger.warn(`Login failed for user: ${username} - Invalid username or password`);
|
||||||
throw new UnauthorizedException('Invalid username or password');
|
throw new UnauthorizedException('Invalid username or password');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.log(`User ${username} logged in successfully`);
|
||||||
|
|
||||||
// 将用户信息附加到请求对象
|
// 将用户信息附加到请求对象
|
||||||
(request as any).user = user;
|
(request as any).user = user;
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ const config = {
|
|||||||
|
|
||||||
const destinations = {
|
const destinations = {
|
||||||
server: '/root/bidding/publish/server',
|
server: '/root/bidding/publish/server',
|
||||||
frontend: '/root/bidding/publish/frontend',
|
frontend: '/root/bidding/publish/frontend/dist',
|
||||||
src: '/root/bidding/',
|
src: '/root/bidding/',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user