添加登陆接口限流

This commit is contained in:
2025-06-17 09:37:24 +08:00
parent 1de3a3f197
commit 2f131e50ee
4 changed files with 29 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ import { BlogModule } from './blog/blog.module';
import { RoleModule } from './role/role.module';
import { AdminModule } from './admin/admin.module';
import { OssModule } from './oss/oss.module';
import { ThrottlerModule } from '@nestjs/throttler';
@Module({
imports: [
@@ -29,6 +30,12 @@ import { OssModule } from './oss/oss.module';
synchronize: process.env.NODE_ENV !== 'production', // Set to false in production
}),
PassportModule.register({ defaultStrategy: 'jwt' }),
ThrottlerModule.forRoot({
throttlers: [{
limit: 1000,
ttl: 60000, // 1 minute
}],
}),
UserModule,
AuthModule,
VerificationModule,
@@ -42,4 +49,4 @@ import { OssModule } from './oss/oss.module';
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
export class AppModule { }

View File

@@ -10,15 +10,18 @@ import { LoginDto } from './dto/login.dto';
import { AuthService } from './auth.service';
import { AuthGuard } from '@nestjs/passport';
import { UserSessionService } from 'src/user/services/user-session.service';
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
@Controller('auth')
export class AuthController {
constructor(
private readonly authService: AuthService,
private readonly userSessionService: UserSessionService,
) {}
) { }
@Post('login')
@UseGuards(ThrottlerGuard)
@Throttle({ default: { limit: 100, ttl: 60000 } })
async login(@Body() loginDto: LoginDto) {
switch (loginDto.type) {
case 'password':