Skip to content

Commit

Permalink
Feature JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
zds-s committed Oct 17, 2024
1 parent d8a4516 commit 00c1080
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
11 changes: 10 additions & 1 deletion .vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { defineConfigWithTheme } from 'vitepress'
import {defineConfigWithTheme, type MarkdownRenderer} from 'vitepress'
import enGetNavs from "./src/en/nav";
import zhGetNavs from "./src/zh/nav";
import enGetConfig from "./src/en/config";
import zhGetConfig from "./src/zh/config";
import zhGetSidebar from "./src/zh/sidebars";
import enGetSidebar from "./src/en/sidebars";
import { AnnouncementPlugin } from 'vitepress-plugin-announcement'
import { plantuml } from "@mdit/plugin-plantuml";


// https://vitepress.dev/reference/site-config
Expand Down Expand Up @@ -118,4 +119,12 @@ export default defineConfigWithTheme ({
}
},
srcDir: 'docs',
markdown:{
config:(md:MarkdownRenderer)=>{
md.use(plantuml,{
type:"fence",
fence:"plantuml",
})
}
}
})
60 changes: 59 additions & 1 deletion docs/zh/backend/base/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,62 @@ MineAdmin 是构建运行在 [PHP](https://php.net) + ([Swoole](https://swoole.c

:::

![时序图](token.png)
---

### 时序图

```plantuml
participant "客户端" as Client
participant "服务端" as Server
Client -> Server : 登录请求
Server -> Client : 登录成功,返回 access_token 和 refresh_token
Client -> Local : 存储 access_token 和 refresh_token 到本地
Client -> Server : 发送请求
Server -> Client : 返回 401 错误码且本地 refresh_token 未过期
Client -> Queue : 暂存请求信息
Client -> Server : 用 refresh_token 换取新 token
alt 换 token 接口返回 401
Client -> Local : 清除本地缓存
Client -> Server : 重定向到登录页面
else 换 token 成功
Client -> Local : 更新本地 token
Client -> Server : 重试请求失败的接口
end
```

### 流程图

```plantuml
start
:登录成功;
->
:存储 access_token 和 refresh_token 到本地;
->
:发送请求;
if (请求失败,code = 401 且本地 refresh_token 未过期) then (yes)
->
:暂存请求信息到队列;
->
:用 refresh_token 换新 token;
if (换 token 接口返回 401) then (yes)
->
:清除本地缓存,重定向到登录页面;
else (no)
->
:更新本地 token,重试请求失败的接口;
endif
else (no)
endif
```

### 讲解

在登录成功后,将 access token 与 refresh token 存储于本地。

当某次请求出现失败,且错误码为 401,同时本地的 refresh_token 未过期时,需先将当前请求信息暂存至队列中。此队列旨在防止同一时刻多个请求同时去刷新 token。

随后,利用 refresh token 换取新的 access_token 与 refresh_token。

倘若换 token 的接口同样返回 401 错误码,则意味着 acces
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"vitepress-plugin-announcement": "^0.1.2"
},
"devDependencies": {
"@mdit/plugin-plantuml": "^0.13.1",
"simple-git": "^3.27.0",
"vitepress": "^1.4.0",
"vitepress-plugin-comment-with-giscus": "^1.1.15",
Expand Down
29 changes: 29 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 00c1080

Please sign in to comment.