Skip to content

Commit 2fa95c9

Browse files
author
litongjava
committed
add Authentication and Authorization
1 parent ed768bc commit 2fa95c9

File tree

4 files changed

+59
-35
lines changed

4 files changed

+59
-35
lines changed

docs/.vuepress/config/sidebar-zh.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"children": ["07_aop/01.md", "07_aop/02.md", "07_aop/03.md", "07_aop/03.md", "07_aop/04.md", "07_aop/05.md"]
7272
},
7373
{
74-
"title": "08_token",
74+
"title": "08_认证和授权",
7575
"collapsable": false,
7676
"children": ["08_token/01.md", "08_token/02.md"]
7777
},
File renamed without changes.

docs/zh/08_token/02.md docs/zh/08_认证/02.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public class AuthController {
138138

139139
public boolean validateToken() {
140140
try {
141-
StpUtil.checkActiveTimeout();
141+
StpUtil.checkLogin();
142142
return true;
143143
} catch (Exception e) {
144144
return false;
@@ -203,6 +203,30 @@ Cookie: token=c5edff10712b4c49b64da5b910ecdb53; PHPSESSID=e11a6f19b3504fec816a3d
203203

204204
也支持请求头参数 token
205205

206+
### 生成 JWT token
207+
208+
添加依赖
209+
210+
```
211+
<!-- Sa-Token 整合 jwt -->
212+
<dependency>
213+
<groupId>cn.dev33</groupId>
214+
<artifactId>sa-token-jwt</artifactId>
215+
<version>1.37.0</version>
216+
</dependency>
217+
```
218+
219+
修改 SaTokenConfiguration 添加 jwt 相关配置
220+
221+
```java
222+
saTokenConfig.setJwtSecretKey("asdasdasifhueuiwyurfewbfjsdafjk");
223+
saTokenConfig.setTokenPrefix("Bearer");
224+
StpLogicJwtForSimple stpLogicJwtForSimple = new StpLogicJwtForSimple();
225+
StpUtil.setStpLogic(stpLogicJwtForSimple);
226+
```
227+
228+
其他不变,正常登录即可
229+
206230
### 将 Token 存入 Redis
207231

208232
#### 为什么要将访问 token 存入 Redis

docs/zh/99_案例/01.md

+33-33
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
使用 sa-token 将用户信息保存到 reids
77
1.1.创建表
88

9-
```
9+
```sql
1010
CREATE TABLE IF NOT EXISTS `sys_user_info` (
1111
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
1212
`app_id` INT NOT NULL COMMENT '应用id',
@@ -50,7 +50,7 @@ CREATE TABLE IF NOT EXISTS `sys_user_info` (
5050
fst
5151
junit
5252

53-
```
53+
```xml
5454
<properties>
5555
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5656
<java.version>1.8</java.version>
@@ -136,7 +136,7 @@ CREATE TABLE IF NOT EXISTS `sys_user_info` (
136136
1.3.配置文件
137137
app.properties
138138

139-
```
139+
```properties
140140
server.port=9204
141141
server.context-path=/pen-api
142142
# 或 prod
@@ -192,7 +192,7 @@ The_username_exists=用户名已经存在
192192

193193
1.4.启动类
194194

195-
```
195+
```java
196196
import com.litongjava.hotswap.wrapper.tio.boot.TioApplicationWrapper;
197197
import com.litongjava.jfinal.aop.annotation.AComponentScan;
198198

@@ -211,7 +211,7 @@ public class MaLiangPenAiServerApp {
211211

212212
1.5.连接 mysql
213213

214-
```
214+
```java
215215
package com.enoleap.manglang.pen.api.server.config;
216216

217217
import javax.sql.DataSource;
@@ -285,7 +285,7 @@ public class TableToJsonConfig {
285285

286286
1.6.连接 reids
287287

288-
```
288+
```java
289289
import com.litongjava.jfinal.aop.annotation.AConfiguration;
290290
import com.litongjava.jfinal.aop.annotation.AInitialization;
291291
import com.litongjava.jfinal.plugin.redis.Cache;
@@ -351,7 +351,7 @@ public class UserRegisterVO {
351351

352352
1.7.2.UserRegisterController
353353

354-
```
354+
```java
355355
import com.litongjava.jfinal.aop.annotation.AAutowired;
356356
import com.litongjava.tio.http.server.annotation.RequestPath;
357357
import com.litongjava.tio.utils.resp.RespVo;
@@ -379,7 +379,7 @@ public class UserRegisterController {
379379

380380
1.7.3.UserRegisteValidator
381381

382-
```
382+
```java
383383
package com.enoleap.manglang.pen.api.server.validator;
384384

385385
import com.enoleap.manglang.pen.api.server.model.UserRegisterVO;
@@ -468,7 +468,7 @@ public class UserRegisteValidator {
468468

469469
1.7.4.SysUserInfoService
470470

471-
```
471+
```java
472472
import com.litongjava.jfinal.plugin.activerecord.Db;
473473

474474
public class SysUserInfoService {
@@ -492,7 +492,7 @@ public class SysUserInfoService {
492492

493493
1.7.5.UserRegisterService
494494

495-
```
495+
```java
496496
import com.litongjava.jfinal.plugin.activerecord.Db;
497497
import com.litongjava.tio.utils.resp.RespVo;
498498

@@ -544,7 +544,7 @@ public class UserRegisterService {
544544
1.8.单元测试注册
545545
1.8.1.TioBootTest
546546

547-
```
547+
```java
548548
import java.util.List;
549549

550550
import com.litongjava.jfinal.aop.Aop;
@@ -572,7 +572,7 @@ public class TioBootTest {
572572

573573
1.8.2.SysUserInfoServiceTest
574574

575-
```
575+
```java
576576
import org.junit.Before;
577577
import org.junit.Test;
578578
import com.litongjava.jfinal.aop.Aop;
@@ -596,7 +596,7 @@ public class SysUserInfoServiceTest {
596596

597597
1.8.3.UserRegisterServiceTest
598598

599-
```
599+
```java
600600
import org.junit.Before;
601601
import org.junit.Test;
602602
import com.litongjava.tio.utils.resp.RespVo;
@@ -625,7 +625,7 @@ public class UserRegisterServiceTest {
625625

626626
1.8.4.UserRegisteValidatorTest
627627

628-
```
628+
```java
629629
import org.junit.Test;
630630

631631
import com.jfinal.kit.StrKit;
@@ -660,7 +660,7 @@ public class UserRegisteValidatorTest {
660660
1.9.整合 sa-token
661661
1.9.1.SaTokenConfiguration
662662

663-
```
663+
```java
664664
// 导入必要的类和注解
665665
import com.litongjava.jfinal.aop.annotation.AConfiguration;
666666
import com.litongjava.jfinal.aop.annotation.AInitialization;
@@ -708,7 +708,7 @@ public class SaTokenConfiguration {
708708

709709
1.9.2.InterceptorConfiguration
710710

711-
```
711+
```java
712712
// 导入必要的类和注解
713713
import com.litongjava.jfinal.aop.annotation.AConfiguration;
714714
import com.litongjava.jfinal.aop.annotation.AInitialization;
@@ -737,7 +737,7 @@ public class InterceptorConfiguration {
737737
1.10.登录
738738
1.10.1.AuthController
739739

740-
```
740+
```java
741741
import java.util.HashMap;
742742
import java.util.Map;
743743

@@ -799,7 +799,7 @@ public class AuthController {
799799

800800
1.10.2.AuthValidator
801801

802-
```
802+
```java
803803
import com.jfinal.kit.StrKit;
804804
import com.litongjava.tio.boot.i18n.I18n;
805805
import com.litongjava.tio.boot.i18n.I18nLocale;
@@ -831,7 +831,7 @@ public class AuthValidator {
831831

832832
1.10.3.AuthService
833833

834-
```
834+
```java
835835
import com.litongjava.jfinal.plugin.activerecord.Db;
836836
import com.litongjava.tio.utils.resp.RespVo;
837837

@@ -856,7 +856,7 @@ public class AuthService {
856856
1.11.发送请求测试
857857
1.11.1.注册
858858

859-
```
859+
```shell
860860
curl --location --request POST 'http://localhost:9204/pen-api/user/register' \
861861
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
862862
--header 'Accept: */*' \
@@ -874,7 +874,7 @@ curl --location --request POST 'http://localhost:9204/pen-api/user/register' \
874874

875875
1.11.2.登录
876876

877-
```
877+
```shell
878878
curl --location --request POST 'http://localhost:9204/pen-api/auth/doLogin' \
879879
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
880880
--header 'Accept: */*' \
@@ -889,15 +889,15 @@ curl --location --request POST 'http://localhost:9204/pen-api/auth/doLogin' \
889889
响应头中保护 token
890890
响应体如下
891891

892-
```
892+
```json
893893
{
894-
"ok": true
894+
"ok": true
895895
}
896896
```
897897

898898
1.11.3.验证 token 是否有效
899899

900-
```
900+
```shell
901901
curl --location --request GET 'http://localhost:9204/pen-api/auth/validateToken' \
902902
--header 'token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
903903
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
@@ -908,7 +908,7 @@ curl --location --request GET 'http://localhost:9204/pen-api/auth/validateToken'
908908

909909
1.11.4.获取用户 id
910910

911-
```
911+
```shell
912912
curl --location --request GET 'http://localhost:9204/pen-api/auth/getUserId' \
913913
--header 'token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
914914
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
@@ -917,20 +917,20 @@ curl --location --request GET 'http://localhost:9204/pen-api/auth/getUserId' \
917917
--header 'Connection: keep-alive'
918918
```
919919

920-
```
920+
```json
921921
{
922-
    "data": {
923-
        "tokenTimeout": 2591819,
924-
        "userId": "35",
925-
        "token": "4eb6ac726a7d4f42bb10fe365823f9f7"
926-
    },
927-
    "ok": true
922+
"data": {
923+
"tokenTimeout": 2591819,
924+
"userId": "35",
925+
"token": "4eb6ac726a7d4f42bb10fe365823f9f7"
926+
},
927+
"ok": true
928928
}
929929
```
930930

931931
1.11.5.登出
932932

933-
```
933+
```shell
934934
curl --location --request GET 'http://localhost:9204/pen-api/auth/logout' \
935935
--header 'token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
936936
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \

0 commit comments

Comments
 (0)