# @boyuai/nestjs-oauth2-server 中文文档

规范地址：https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/

版本：0\.1\.0
npm 元数据更新时间：2022-04-04T20:18:34.056Z
中文译文更新时间：2026-11-08T14:38:36.438Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40boyuai/nestjs-oauth2-server
原文对照：https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/?lang=original
项目主页：https://github.com/boyuai/nestjs-oauth2-server#readme
源码仓库：https://github.com/boyuai/nestjs-oauth2-server

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：48.3 kB，39 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@nestjs/common ^6\.10\.0 \|\| ^7\.0\.0 \|\| ^8\.0\.10，reflect-metadata ^0\.1\.12，rxjs ^6\.0\.0 \|\| ^7\.2\.0
- 首次发布：2021-11-20T08:56:35.838Z
- 最新版本发布：2022-02-26T14:48:39.925Z
- 近 30 天下载量：1792（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 0 个）

## 中文简介

Oauth2- 服务器的 NestJS 包装器

## 安装

```sh
npm install @boyuai/nestjs-oauth2-server
```

也可以使用：`pnpm add @boyuai/nestjs-oauth2-server`、`yarn add @boyuai/nestjs-oauth2-server`、`bun add @boyuai/nestjs-oauth2-server`

## README

## NestJS OAuth2 服务器

[![Nest Logo](https://nestjs.com/img/logo-small.svg)](http://nestjs.com/) ![OAuth2 Logo](https://github.com/boyuai/nestjs-oauth2-server/blob/master/oauth2.png?raw=true)

[![npm](https://img.shields.io/npm/dm/@boyuai/nestjs-oauth2-server)](https://www.npmjs.com/package/@boyuai/nestjs-oauth2-server) [![npm version](https://img.shields.io/npm/v/@boyuai/nestjs-oauth2-server?label=NPM&logo=NPM)](https://npmjs.com/@boyuai/nestjs-oauth2-server) [![LICENCE](https://img.shields.io/npm/l/@boyuai/nestjs-oauth2-server)](https://npmjs.com/@boyuai/nestjs-oauth2-server) [![synk vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/@boyuai/nestjs-oauth2-server)](https://www.npmjs.com/package/@boyuai/nestjs-oauth2-server)

A 类 [网点JS](https://nestjs.com/) 用于 [oaut2 服务器](https://oauth2-server.readthedocs.io/en/latest/index.html) 软件包.

**目录** (单击以扩充)

-   [安装](https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/#readme-installation)
-   [配置](https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/#readme-configuration)
-   [使用量](https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/#readme-usage)
-   [学习](https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/#readme-learnings)
-   [捐款](https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/#readme-contributing)

## 安装

安装和运行一样简单 :

`npm install @boyuai/nestjs-oauth2-server`

或者说

`yarn add @boyuai/nestjs-oauth2-server`。 。 。 .

## 配置

1.  将模块作为依赖性纳入将发生oauth的模块:

`oauth2.module.ts`

```ts
import { Module } from '@nestjs/common';
import { OAuth2ServerModule } from '@boyuai/nestjs-oauth2-server';
import { OAuth2Controller } from './oauth2.controller';
import { OAuth2ServiceModule } from './oauth2-service.module';
import { OAuth2Model } from './oauth2.model';

@Module({
  imports: [
    OAuth2ServiceModule,
    OAuth2ServerModule.forRoot({
      imports: [OAuth2ServiceModule], // import your repository for OAuth2Model here
      modelClass: OAuth2Model,
    }),
  ],
  controllers: [OAuth2Controller],
})
export class OAuth2Module {}
```

除了上述, **oaut2 服务器** 需要一个 [模式](https://oauth2-server.readthedocs.io/en/latest/model/overview.html) 创建服务器。 这可作为申请的任何部分提供的服务。 这应能获取客户端、用户、信使和授权代码的数据。 这个 **必须** 成为装饰于 `Injectable` 装饰师.

`oauth2.model.ts`

```ts
import { Injectable } from '@nestjs/common';

@Injectable()
export class OAuth2Model implements RequestAuthenticationModel {
    getAccessToken() {}

    verifyScope() {}
    // ...
    // check more codes in test/src/test-model.service.ts
}
```

## 使用量

该模块还提供一些精美的装饰来帮助配置oauth2处理器端点. 下面给出一个覆盖全部装潢阵列的示例控制器

`oauth2.controller.ts`

```ts
import { Controller } from '@nestjs/common';
import {
  OAuth2Authorization,
  OAuth2Authorize,
  OAuth2RenewToken,
  OAuth2Token,
} from '@boyuai/nestjs-oauth2-server';

@Controller()
export class OAuth2Controller {
    @Post()
    @OAuth2Authenticate()
    authenticateClient(@OAuth2Token() token: Token) {
        return token;
    }

    @Post()
    @OAuth2Authorize()
    authorizeClient(
        @OAuth2Authorization()
        authorization: AuthorizationCode,
    ) {
        return authorization;
    }

    @Post()
    @OAuth2RenewToken()
    renewToken(@OAuth2Token() token: Token) {
        return token;
    }
}
```

## Async 配置

该模块也可同步使用 `forRootAsync` 方法.

实例如下:

-   采用工厂供应商办法

```ts
import { Module } from '@nestjs/common';
import {
    OAuth2ServerModule,
    IOAuth2ServerModuleOptions,
} from '@boyuai/nestjs-oauth2-server';

@Module({
    imports: [
        // ... other modules
        OAuth2ServerModule.forRootAsync({
            useFactory: (factory: IOAuth2ServerOptionsFactory) => ({}),
            modelClass: YourModelService,
        }),
    ],
})
export class YourModule {}
```

-   采用类或现有提供者办法:

`oauth2-server-config.service.ts`

```ts
import {
    IOAuth2ServerModuleOptions,
    IOAuth2ServerOptionsFactory,
} from '@boyuai/nestjs-oauth2-server';
import { Injectable } from '@nestjs/common';

@Injectable()
export class OAuth2ServerConfigService
    implements IOAuth2ServerOptionsFactory {
    createOAuth2ServerOptions(): IOAuth2ServerModuleOptions {
        return {};
    }
}
```

这个 `OAuth2ServerConfigService` **应该** 执行 `IOAuth2ServerOptionsFactory`, (中文(简体) ), **必须** 声明 `createOAuth2ServerOptions` 方法和 **必须** 返回时 `IOAuth2ServerModuleOptions` 对象.

```ts
import { Module } from '@nestjs/common';
import { OAuth2ServerModule } from '@boyuai/nestjs-oauth2-server';
import { OAuth2ServerConfigService } from './oauth2-server-config.service.ts';

@Module({
    imports: [
        // ... other modules
        OAuth2ServerModule.forRootAsync({
            useClass: OAuth2ServerConfigService,
            modelClass: YourModelService,
        }),
    ],
})
export class YourModule {}
```

## 学习

本条可进一步理解OAuth2的概念 [这里](https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2)。也可以去Oauth2服务器包 [文档](https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/package)。 。 。 .

## 捐款

但是,欢迎关于改进的建议,请遵守《公约》 [捐款](https://npmcn.com/package/%40boyuai/nestjs-oauth2-server/CONTRIBUTING.md) 准则.

## 常见问题

### @boyuai/nestjs-oauth2-server 是什么？

Oauth2- 服务器的 NestJS 包装器

### 如何安装 @boyuai/nestjs-oauth2-server？

在项目目录运行 npm install @boyuai/nestjs-oauth2-server，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@nestjs/common（^6\.10\.0 \|\| ^7\.0\.0 \|\| ^8\.0\.10）、reflect-metadata（^0\.1\.12）、rxjs（^6\.0\.0 \|\| ^7\.2\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @boyuai/nestjs-oauth2-server 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @boyuai/nestjs-oauth2-server 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @boyuai/nestjs-oauth2-server 可以商用吗？

@boyuai/nestjs-oauth2-server 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @boyuai/nestjs-oauth2-server 的下载量有多少？

根据 npm 官方下载统计，@boyuai/nestjs-oauth2-server 在 2026/10/09 – 2026/11/07 这 30 天内被下载 1,792 次，日均约 60 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @boyuai/nestjs-oauth2-server 还在维护吗？

最新版本 0\.1\.0 发布于 2022/02/26（4 年前）；至今共发布 2 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/11/20。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

