# @antelopejs/auth-jwt 中文文档

规范地址：https://npmcn.com/package/%40antelopejs/auth-jwt/

版本：1\.1\.0
npm 元数据更新时间：2026-11-04T21:15:28.923Z
中文译文更新时间：2026-11-06T06:44:30.574Z
软件包许可：Apache-2\.0

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

原始 npm 页面：https://www.npmjs.com/package/%40antelopejs/auth-jwt
原文对照：https://npmcn.com/package/%40antelopejs/auth-jwt/?lang=original
项目主页：https://interfaces.antelopejs.com/docs/auth/introduction
源码仓库：https://github.com/AntelopeJS/auth-jwt

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

- 包体积（解压后）：42.2 kB，18 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：5 个
- 首次发布：2025-05-08T22:50:37.634Z
- 最新版本发布：2026-11-04T21:15:28.606Z
- 近 30 天下载量：4398（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：12（近 12 个月 9 个）

## 中文简介

羚羊JS的JWT认证模块,与 jsonwebtoken 执行 Auth 接口

## 安装

```sh
npm install @antelopejs/auth-jwt
```

也可以使用：`pnpm add @antelopejs/auth-jwt`、`yarn add @antelopejs/auth-jwt`、`bun add @antelopejs/auth-jwt`

## README

# `@antelopejs/auth-jwt`

[![npm](https://img.shields.io/npm/v/@antelopejs/auth-jwt?style=for-the-badge&labelColor=000000&color=000000)](https://www.npmjs.com/package/@antelopejs/auth-jwt) [![License](https://img.shields.io/npm/l/@antelopejs/auth-jwt?style=for-the-badge&labelColor=000000&color=000000)](https://npmcn.com/package/%40antelopejs/auth-jwt/LICENSE) [![Discord](https://img.shields.io/badge/Discord-18181B?logo=discord&style=for-the-badge&color=000000)](https://discord.gg/g4pFt6eXqu) [![Documentation](https://img.shields.io/badge/Docs-18181B?style=for-the-badge&color=000000)](https://interfaces.antelopejs.com/docs/auth/introduction)

羚羊JS认证模块. 执行 `@antelopejs/interface-auth` 并用JSON Web Tokens来标记和验证其标志 [`jsonwebtoken`](https://github.com/auth0/node-jsonwebtoken)。 。 。 .

部分 [羚羊JS](https://github.com/AntelopeJS/antelopejs)用于 [羚羊JS 隐形物](https://github.com/AntelopeJS/dms),用于您的 PostgreSQL 、 MongoDB 或 RethinkDB 数据库的开源后台.

## 接口

| 接口 | 文档 |
| --- | --- |
| [`@antelopejs/interface-auth`](https://github.com/AntelopeJS/interface-auth) | [认证](https://interfaces.antelopejs.com/docs/auth/introduction) |

界面页面记录 `@Authentication` 装饰器、自定义认证器、信使源和饼干 模块还取决于 [`@antelopejs/interface-api`](https://interfaces.antelopejs.com/docs/api/introduction),哪个 [`@antelopejs/api`](https://github.com/AntelopeJS/api) 执行.

## 安装

```bash
ajs project modules add @antelopejs/auth-jwt
ajs project modules install
```

`ajs project modules install` 添加 `@antelopejs/api` 当项目中还没有模块执行 API 接口时.

## 配置

用一个共享的秘密签名 :

```typescript
import { defineConfig } from "@antelopejs/interface-core/config";

export default defineConfig({
  name: "my-project",
  modules: {
    "auth-jwt": {
      source: {
        type: "package",
        package: "@antelopejs/auth-jwt",
        version: "^2.0.0",
      },
      config: {
        secret: "change-me",
        signOptions: { expiresIn: "1h" },
        verifyOptions: { ignoreExpiration: false },
      },
    },
  },
  envOverrides: {
    JWT_SECRET: "modules.auth-jwt.config.secret",
  },
});
```

当 `JWT_SECRET` 环境变量被设定, `envOverrides` 将其值写入 `secret`。 。 。 .

要用密钥对来签名, 请给出两个密钥文件的路径 。 模块构造时会读取两个文件.

```typescript
config: {
  signKey: "./keys/private.pem",
  verifyKey: "./keys/public.pem",
  signOptions: { algorithm: "RS256", expiresIn: "1h" },
  verifyOptions: { algorithms: ["RS256"] },
},
```

| 密钥 | 说明 |
| --- | --- |
| `secret` | 共同的秘密, 标志和验证的标志。 胜过 `signKey` 财务报告和审定财务报表 `verifyKey`。 。 。 . |
| `signKey` | 私钥文件的路径 。 模块使用它与 `verifyKey` 何时 `secret` 未设置. |
| `verifyKey` | 公钥文件的路径 . |
| `signOptions` | 默认 [`jsonwebtoken` 签名选项](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback)编辑 `expiresIn` 财务报告和审定财务报表 `notBefore` 那个 `SignRaw` 或者说 `SignServerResponse` 接收它们. |
| `verifyOptions` | 默认 [`jsonwebtoken` 校验选项](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback)选项 `ValidateRaw` 超过他们. |

模块拒绝启动 `secret` 或两者兼有 `signKey` 财务报告和审定财务报表 `verifyKey`投掷 `Invalid JWT config`。 。 。 . `ajs project modules add` 写入 `secret: "your-secret-key"`在部署前替换它.

## 快开始

在服务路线的模块中,添加两个接口.

```bash
pnpm add @antelopejs/interface-api @antelopejs/interface-auth
```

以饼干的形式发出一个令牌,然后在受保护的路线上读回.

```typescript
import {
  Context,
  Controller,
  Get,
  Post,
  type RequestContext,
} from "@antelopejs/interface-api";
import { Authentication, SignServerResponse } from "@antelopejs/interface-auth";

interface Session {
  userId: string;
}

export class SessionController extends Controller("/session") {
  @Post("/login")
  async login(@Context() context: RequestContext) {
    await SignServerResponse(
      context.rawResponse,
      { userId: "42" },
      { expiresIn: "1h" },
      { httpOnly: true, path: "/" },
    );
    return { ok: true };
  }

  @Get("/me")
  me(@Authentication() session: Session) {
    return session;
  }
}
```

`GET /session/me` 从 `x-antelopejs-auth` 标题,或来自 `ANTELOPEJS_AUTH` 当信头缺席时做曲奇. 它会回答的 `401` 当它不见了。 它会回答的 `403` 当符号无效或过期,或者其有效载荷既不是字符串,也不是JSON对象,如数组或数字.

## 文档

-   [认证](https://interfaces.antelopejs.com/docs/auth/introduction) 文档 `@antelopejs/interface-auth` 与本模块执行的合同.
-   [配置](https://framework.antelopejs.com/docs/concepts/configuration) 文档模块来源, `config` 区块和配置变量 `antelope.config.ts`。 。 。 .

## 兼容性

| 软件包 | 支助 | 测试过 |
| --- | --- | --- |
| `@antelopejs/interface-api` | `>=0.2.0 <1.0.0` | `0.2.0` |
| `@antelopejs/interface-auth` | `>=0.2.0 <0.3.0` | `0.2.0` |
| `@antelopejs/interface-core` | `>=0.4.0 <1.0.0` | `0.4.0` |

生成的表格列出接口软件包,并将此模块负载范围从 `package.json`。 。 。 .

## 捐款

```bash
pnpm install
pnpm dev            # run the playground/ project in watch mode
pnpm lint
pnpm format:check
pnpm knip
pnpm test           # build, then ajs module test
```

读取 [捐款准则](https://npmcn.com/package/%40antelopejs/auth-jwt/CONTRIBUTING.md) 在打开拉出请求之前.

## 许可证

[阿帕奇 - 2.0](https://npmcn.com/package/%40antelopejs/auth-jwt/LICENSE)

## 常见问题

### @antelopejs/auth-jwt 是什么？

羚羊JS的JWT认证模块,与 jsonwebtoken 执行 Auth 接口

### 如何安装 @antelopejs/auth-jwt？

在项目目录运行 npm install @antelopejs/auth-jwt，或使用 pnpm add / yarn add / bun add。

### @antelopejs/auth-jwt 支持 TypeScript 吗？

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

### @antelopejs/auth-jwt 支持 ESM 还是 CommonJS？

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

### @antelopejs/auth-jwt 可以商用吗？

@antelopejs/auth-jwt 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @antelopejs/auth-jwt 的下载量有多少？

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

### @antelopejs/auth-jwt 还在维护吗？

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

