nestjs 只在UserModule中设置了全局守卫 为什么所有的路由都需要守卫验证了 并么有在根模块设置全局首位
时间: 2024-03-28 08:38:04 浏览: 26
可能是因为在NestJS中,全局守卫是在应用程序的根模块中注册的。如果您在UserModule中设置了全局守卫,则这意味着UserModule是应用程序的根模块,因此所有路由都会使用该全局守卫。如果您想为其他模块设置不同的全局守卫,可以在其他模块中注册不同的守卫。但是,如果您想要在应用程序的不同模块中使用相同的全局守卫,则可以将其注册在应用程序的根模块中。
相关问题
nestjs 如何在不用的模块中设置不同的全局守卫 使其不互相影响
在 NestJS 中,您可以在每个模块中设置不同的全局守卫,以使它们不相互影响。您可以通过在模块中使用 `@UseGuards()` 装饰器来设置全局守卫。这将覆盖在应用程序的根模块中设置的全局守卫。
例如,假设您有两个模块:`UserModule` 和 `AdminModule`。您可以在 `UserModule` 中设置一个全局守卫,而在 `AdminModule` 中设置另一个全局守卫。这可以通过以下方式完成:
```typescript
// user.module.ts
import { Module } from '@nestjs/common';
import { AuthGuard } from './auth.guard';
import { UserController } from './user.controller';
@Module({
controllers: [UserController],
providers: [],
})
@UseGuards(AuthGuard) // 设置 UserModule 的全局守卫
export class UserModule {}
// admin.module.ts
import { Module } from '@nestjs/common';
import { AdminAuthGuard } from './admin-auth.guard';
import { AdminController } from './admin.controller';
@Module({
controllers: [AdminController],
providers: [],
})
@UseGuards(AdminAuthGuard) // 设置 AdminModule 的全局守卫
export class AdminModule {}
```
在上面的代码中,`UserModule` 使用 `AuthGuard` 作为其全局守卫,而 `AdminModule` 使用 `AdminAuthGuard` 作为其全局守卫。这将确保每个模块都有自己的全局守卫,并且它们不会相互干扰。
nestjs typeorm.forroot()不用在根模块的情况下 下面的prividers和controller如何设置
如果你在根模块中使用了 `TypeOrmModule.forRoot()`,则你可以在任何一个子模块中使用 `TypeOrmModule` 来连接到数据库,而不需要再次调用 `forRoot()`,否则会报错。在子模块中,你可以设置自己的 `providers` 和 `controllers`,它们可以访问到来自 `TypeOrmModule` 的依赖注入。以下是一个示例:
```typescript
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserController } from './user.controller';
import { UserService } from './user.service';
import { UserEntity } from './user.entity';
@Module({
imports: [
TypeOrmModule.forFeature([UserEntity]),
],
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
```
在上面的示例中,`TypeOrmModule.forFeature()` 方法用于导入与 `UserEntity` 相关的实体类,以便它们可以被 `UserService` 和 `UserController` 访问。在 `providers` 中,`UserService` 依赖于 `TypeOrmService`,并通过依赖注入来访问数据库连接。在 `controllers` 中,`UserController` 使用 `UserService` 服务来处理 HTTP 请求。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)