diff --git a/package.json b/package.json index 62e8d0b..05476e4 100644 --- a/package.json +++ b/package.json @@ -23,11 +23,13 @@ "@nestjs/common": "^10.0.0", "@nestjs/config": "^3.3.0", "@nestjs/core": "^10.0.0", + "@nestjs/jwt": "^10.2.0", "@nestjs/platform-express": "^10.0.0", "@nestjs/swagger": "^8.0.7", "@nestjs/typeorm": "^10.0.2", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", + "jsonwebtoken": "^9.0.2", "mysql2": "^3.11.4", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1751370..d0904a9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: '@nestjs/core': specifier: ^10.0.0 version: 10.4.7(@nestjs/common@10.4.7(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1))(@nestjs/platform-express@10.4.7)(reflect-metadata@0.2.2)(rxjs@7.8.1) + '@nestjs/jwt': + specifier: ^10.2.0 + version: 10.2.0(@nestjs/common@10.4.7(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1)) '@nestjs/platform-express': specifier: ^10.0.0 version: 10.4.7(@nestjs/common@10.4.7(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1))(@nestjs/core@10.4.7) @@ -32,6 +35,9 @@ importers: class-validator: specifier: ^0.14.1 version: 0.14.1 + jsonwebtoken: + specifier: ^9.0.2 + version: 9.0.2 mysql2: specifier: ^3.11.4 version: 3.11.4 @@ -498,6 +504,11 @@ packages: '@nestjs/websockets': optional: true + '@nestjs/jwt@10.2.0': + resolution: {integrity: sha512-x8cG90SURkEiLOehNaN2aRlotxT0KZESUliOPKKnjWiyJOcWurkF3w345WOX0P4MgFzUjGoZ1Sy0aZnxeihT0g==} + peerDependencies: + '@nestjs/common': ^8.0.0 || ^9.0.0 || ^10.0.0 + '@nestjs/mapped-types@2.0.6': resolution: {integrity: sha512-84ze+CPfp1OWdpRi1/lOu59hOhTz38eVzJvRKrg9ykRFwDz+XleKfMsG0gUqNZYFa6v53XYzeD+xItt8uDW7NQ==} peerDependencies: @@ -670,6 +681,9 @@ packages: '@types/json-schema@7.0.15': resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==} + '@types/jsonwebtoken@9.0.5': + resolution: {integrity: sha512-VRLSGzik+Unrup6BsouBeHsf4d1hOEgYWTm/7Nmw1sXoN1+tRly/Gy/po3yeahnP4jfnQWWAhQAqcNfH7ngOkA==} + '@types/methods@1.1.4': resolution: {integrity: sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==} @@ -1002,6 +1016,9 @@ packages: bser@2.1.1: resolution: {integrity: sha512-gQxTNE/GAfIIrmHLUE3oJyp5FO6HRBfhjnw4/wMmA63ZGDJnWBmgY/lyQBpnDUkGmAhbSe39tx2d/iTOAfglwQ==} + buffer-equal-constant-time@1.0.1: + resolution: {integrity: sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==} + buffer-from@1.1.2: resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} @@ -1297,6 +1314,9 @@ packages: eastasianwidth@0.2.0: resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} + ecdsa-sig-formatter@1.0.11: + resolution: {integrity: sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==} + ee-first@1.1.1: resolution: {integrity: sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==} @@ -1983,6 +2003,16 @@ packages: jsonfile@6.1.0: resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==} + jsonwebtoken@9.0.2: + resolution: {integrity: sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==} + engines: {node: '>=12', npm: '>=6'} + + jwa@1.4.1: + resolution: {integrity: sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==} + + jws@3.2.2: + resolution: {integrity: sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==} + keyv@4.5.4: resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==} @@ -2016,12 +2046,33 @@ packages: resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==} engines: {node: '>=10'} + lodash.includes@4.3.0: + resolution: {integrity: sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==} + + lodash.isboolean@3.0.3: + resolution: {integrity: sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==} + + lodash.isinteger@4.0.4: + resolution: {integrity: sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==} + + lodash.isnumber@3.0.3: + resolution: {integrity: sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==} + + lodash.isplainobject@4.0.6: + resolution: {integrity: sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==} + + lodash.isstring@4.0.1: + resolution: {integrity: sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==} + lodash.memoize@4.1.2: resolution: {integrity: sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==} lodash.merge@4.6.2: resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==} + lodash.once@4.1.1: + resolution: {integrity: sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==} + lodash@4.17.21: resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==} @@ -3555,6 +3606,12 @@ snapshots: transitivePeerDependencies: - encoding + '@nestjs/jwt@10.2.0(@nestjs/common@10.4.7(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1))': + dependencies: + '@nestjs/common': 10.4.7(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1) + '@types/jsonwebtoken': 9.0.5 + jsonwebtoken: 9.0.2 + '@nestjs/mapped-types@2.0.6(@nestjs/common@10.4.7(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1))(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)': dependencies: '@nestjs/common': 10.4.7(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1) @@ -3746,6 +3803,10 @@ snapshots: '@types/json-schema@7.0.15': {} + '@types/jsonwebtoken@9.0.5': + dependencies: + '@types/node': 20.17.6 + '@types/methods@1.1.4': {} '@types/mime@1.3.5': {} @@ -4166,6 +4227,8 @@ snapshots: dependencies: node-int64: 0.4.0 + buffer-equal-constant-time@1.0.1: {} + buffer-from@1.1.2: {} buffer@5.7.1: @@ -4429,6 +4492,10 @@ snapshots: eastasianwidth@0.2.0: {} + ecdsa-sig-formatter@1.0.11: + dependencies: + safe-buffer: 5.2.1 + ee-first@1.1.1: {} ejs@3.1.10: @@ -5373,6 +5440,30 @@ snapshots: optionalDependencies: graceful-fs: 4.2.11 + jsonwebtoken@9.0.2: + dependencies: + jws: 3.2.2 + lodash.includes: 4.3.0 + lodash.isboolean: 3.0.3 + lodash.isinteger: 4.0.4 + lodash.isnumber: 3.0.3 + lodash.isplainobject: 4.0.6 + lodash.isstring: 4.0.1 + lodash.once: 4.1.1 + ms: 2.1.3 + semver: 7.6.3 + + jwa@1.4.1: + dependencies: + buffer-equal-constant-time: 1.0.1 + ecdsa-sig-formatter: 1.0.11 + safe-buffer: 5.2.1 + + jws@3.2.2: + dependencies: + jwa: 1.4.1 + safe-buffer: 5.2.1 + keyv@4.5.4: dependencies: json-buffer: 3.0.1 @@ -5400,10 +5491,24 @@ snapshots: dependencies: p-locate: 5.0.0 + lodash.includes@4.3.0: {} + + lodash.isboolean@3.0.3: {} + + lodash.isinteger@4.0.4: {} + + lodash.isnumber@3.0.3: {} + + lodash.isplainobject@4.0.6: {} + + lodash.isstring@4.0.1: {} + lodash.memoize@4.1.2: {} lodash.merge@4.6.2: {} + lodash.once@4.1.1: {} + lodash@4.17.21: {} log-symbols@4.1.0: diff --git a/src/core/constants/jwt.ts b/src/core/constants/jwt.ts new file mode 100644 index 0000000..ff8629e --- /dev/null +++ b/src/core/constants/jwt.ts @@ -0,0 +1,2 @@ +export const JWT_SECRET = 'jwtSecret'; +export const JWT_EXPIRATION = '1h'; diff --git a/src/core/controller/token.controller.ts b/src/core/controller/token.controller.ts new file mode 100644 index 0000000..944960d --- /dev/null +++ b/src/core/controller/token.controller.ts @@ -0,0 +1,12 @@ +import { Body, Controller, Post } from '@nestjs/common'; +import { TokenCreateRequest } from '../dto/token-create.request'; +import { NoAuth } from '../decorators/no-auth.decorator'; + +@Controller('/tokens') +export class TokenController { + @Post() + @NoAuth() + create(@Body() tokenCreateRequest: TokenCreateRequest) { + return tokenCreateRequest.username; + } +} diff --git a/src/core/core.module.ts b/src/core/core.module.ts index 1f27cd2..30efb58 100644 --- a/src/core/core.module.ts +++ b/src/core/core.module.ts @@ -1,8 +1,15 @@ import { Global, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { ConfigModule, ConfigService } from '@nestjs/config'; +import { TokenController } from './controller/token.controller'; +import { UserService } from './service/user.service'; +import { User } from './entity/user.entity'; +import { APP_GUARD } from '@nestjs/core'; +import { AuthGuard } from './guards/auth.guard'; +import { JwtService } from '@nestjs/jwt'; @Module({ + controllers: [TokenController], imports: [ ConfigModule.forRoot({ isGlobal: true, @@ -21,11 +28,23 @@ import { ConfigModule, ConfigService } from '@nestjs/config'; password: configService.get('DB_PASSWORD'), database: configService.get('DB_NAME'), autoLoadEntities: true, + entities: [__dirname + '/**/*.entity{.ts,.js}'], synchronize: process.env.NODE_ENV === 'development', + logging: true, }), inject: [ConfigService], }), + TypeOrmModule.forFeature([User]), ], + providers: [ + { + provide: APP_GUARD, + useClass: AuthGuard, + }, + UserService, + JwtService, + ], + exports: [UserService], }) @Global() export class CoreModule {} diff --git a/src/core/decorators/no-auth.decorator.ts b/src/core/decorators/no-auth.decorator.ts new file mode 100644 index 0000000..721fa2a --- /dev/null +++ b/src/core/decorators/no-auth.decorator.ts @@ -0,0 +1,4 @@ +import { SetMetadata } from '@nestjs/common'; + +export const NO_AUTH_KEY = 'noAuth'; +export const NoAuth = () => SetMetadata(NO_AUTH_KEY, true); diff --git a/src/core/dto/token-create.request.ts b/src/core/dto/token-create.request.ts new file mode 100644 index 0000000..6fd3992 --- /dev/null +++ b/src/core/dto/token-create.request.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty } from 'class-validator'; + +export class TokenCreateRequest { + @ApiProperty({ + required: true, + }) + @IsNotEmpty({ message: '用户名不能为空' }) + username: string; + + @IsNotEmpty({ message: '密码不能为空' }) + @ApiProperty({ + required: true, + }) + password: string; +} diff --git a/src/core/entity/traceable.entity.ts b/src/core/entity/traceable.entity.ts index 1ea1480..9f4a10f 100644 --- a/src/core/entity/traceable.entity.ts +++ b/src/core/entity/traceable.entity.ts @@ -1,8 +1,7 @@ import { BaseEntity } from './base.entity'; -import { Entity, ManyToOne } from 'typeorm'; +import { ManyToOne } from 'typeorm'; import { User } from './user.entity'; -@Entity() export abstract class TraceableEntity extends BaseEntity { @ManyToOne(() => User) createBy: User; diff --git a/src/core/guards/auth.guard.ts b/src/core/guards/auth.guard.ts new file mode 100644 index 0000000..0a95d59 --- /dev/null +++ b/src/core/guards/auth.guard.ts @@ -0,0 +1,61 @@ +import { + CanActivate, + ExecutionContext, + Injectable, + UnauthorizedException, +} from '@nestjs/common'; +import { Reflector } from '@nestjs/core'; +import { JwtService } from '@nestjs/jwt'; +import { UserService } from '../service/user.service'; +import { NO_AUTH_KEY } from '../decorators/no-auth.decorator'; +import { JWT_SECRET } from '../constants/jwt'; + +@Injectable() +export class AuthGuard implements CanActivate { + constructor( + private readonly reflector: Reflector, + private readonly jwtService: JwtService, + private readonly userService: UserService, // 注入 UserService + ) {} + async canActivate(context: ExecutionContext) { + const isNoAuth = this.reflector.getAllAndOverride(NO_AUTH_KEY, [ + context.getHandler(), + context.getClass(), + ]); + + if (isNoAuth) { + return true; + } + // 获取请求对象 + const request = context.switchToHttp().getRequest(); + const token = this.extractTokenFromHeader(request); + if (!token) { + throw new UnauthorizedException('Missing authorization token'); + } + try { + // 验证并解析 JWT + const payload = await this.jwtService.verifyAsync(token, { + secret: JWT_SECRET, + }); + + // 从 UserService 加载用户 + const user = await this.userService.findById(payload.sub); + if (!user) { + throw new UnauthorizedException('User not found'); + } + + // 将用户信息附加到请求对象 + request.user = user; + return true; + } catch (error) { + throw new UnauthorizedException('Invalid token'); + } + } + private extractTokenFromHeader(request: any): string | null { + const authHeader = request.headers['Authorization']; + if (!authHeader || !authHeader.startsWith('Bearer ')) { + return null; + } + return authHeader.split(' ')[1]; + } +} diff --git a/src/core/service/user.service.ts b/src/core/service/user.service.ts new file mode 100644 index 0000000..9085520 --- /dev/null +++ b/src/core/service/user.service.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { User } from '../entity/user.entity'; +import { Repository } from 'typeorm'; + +@Injectable() +export class UserService { + constructor( + @InjectRepository(User) private readonly userRepository: Repository, + ) {} + async findById(id: string) { + return this.userRepository.findOne({ + where: { + id, + }, + }); + } +} diff --git a/src/main.ts b/src/main.ts index fae31bc..0bafe54 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,10 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { ValidationPipe } from '@nestjs/common'; async function bootstrap() { + SnowflakeUtil.initialize( + parseInt(process.env.WORKER_ID, 10), + parseInt(process.env.DATACENTER_ID, 10), + ); const app = await NestFactory.create(AppModule); if (['development', 'test'].includes(process.env.NODE_ENV)) { const config = new DocumentBuilder() @@ -15,10 +19,7 @@ async function bootstrap() { const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('docs', app, document); } - SnowflakeUtil.initialize( - parseInt(process.env.WORKER_ID, 10), - parseInt(process.env.DATACENTER_ID, 10), - ); + app.useGlobalPipes( new ValidationPipe({ transform: true,