6
6
使用 sa-token 将用户信息保存到 reids
7
7
1.1.创建表
8
8
9
- ```
9
+ ``` sql
10
10
CREATE TABLE IF NOT EXISTS ` sys_user_info` (
11
11
` id` BIGINT NOT NULL AUTO_INCREMENT COMMENT ' 主键' ,
12
12
` app_id` INT NOT NULL COMMENT ' 应用id' ,
@@ -50,7 +50,7 @@ CREATE TABLE IF NOT EXISTS `sys_user_info` (
50
50
fst
51
51
junit
52
52
53
- ```
53
+ ``` xml
54
54
<properties >
55
55
<project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
56
56
<java .version>1.8</java .version>
@@ -136,7 +136,7 @@ CREATE TABLE IF NOT EXISTS `sys_user_info` (
136
136
1.3.配置文件
137
137
app.properties
138
138
139
- ```
139
+ ``` properties
140
140
server.port =9204
141
141
server.context-path =/pen-api
142
142
# 或 prod
@@ -192,7 +192,7 @@ The_username_exists=用户名已经存在
192
192
193
193
1.4.启动类
194
194
195
- ```
195
+ ``` java
196
196
import com.litongjava.hotswap.wrapper.tio.boot.TioApplicationWrapper ;
197
197
import com.litongjava.jfinal.aop.annotation.AComponentScan ;
198
198
@@ -211,7 +211,7 @@ public class MaLiangPenAiServerApp {
211
211
212
212
1.5.连接 mysql
213
213
214
- ```
214
+ ``` java
215
215
package com.enoleap.manglang.pen.api.server.config ;
216
216
217
217
import javax.sql.DataSource ;
@@ -285,7 +285,7 @@ public class TableToJsonConfig {
285
285
286
286
1.6.连接 reids
287
287
288
- ```
288
+ ``` java
289
289
import com.litongjava.jfinal.aop.annotation.AConfiguration ;
290
290
import com.litongjava.jfinal.aop.annotation.AInitialization ;
291
291
import com.litongjava.jfinal.plugin.redis.Cache ;
@@ -351,7 +351,7 @@ public class UserRegisterVO {
351
351
352
352
1.7.2.UserRegisterController
353
353
354
- ```
354
+ ``` java
355
355
import com.litongjava.jfinal.aop.annotation.AAutowired ;
356
356
import com.litongjava.tio.http.server.annotation.RequestPath ;
357
357
import com.litongjava.tio.utils.resp.RespVo ;
@@ -379,7 +379,7 @@ public class UserRegisterController {
379
379
380
380
1.7.3.UserRegisteValidator
381
381
382
- ```
382
+ ``` java
383
383
package com.enoleap.manglang.pen.api.server.validator ;
384
384
385
385
import com.enoleap.manglang.pen.api.server.model.UserRegisterVO ;
@@ -468,7 +468,7 @@ public class UserRegisteValidator {
468
468
469
469
1.7.4.SysUserInfoService
470
470
471
- ```
471
+ ``` java
472
472
import com.litongjava.jfinal.plugin.activerecord.Db ;
473
473
474
474
public class SysUserInfoService {
@@ -492,7 +492,7 @@ public class SysUserInfoService {
492
492
493
493
1.7.5.UserRegisterService
494
494
495
- ```
495
+ ``` java
496
496
import com.litongjava.jfinal.plugin.activerecord.Db ;
497
497
import com.litongjava.tio.utils.resp.RespVo ;
498
498
@@ -544,7 +544,7 @@ public class UserRegisterService {
544
544
1.8.单元测试注册
545
545
1.8.1.TioBootTest
546
546
547
- ```
547
+ ``` java
548
548
import java.util.List ;
549
549
550
550
import com.litongjava.jfinal.aop.Aop ;
@@ -572,7 +572,7 @@ public class TioBootTest {
572
572
573
573
1.8.2.SysUserInfoServiceTest
574
574
575
- ```
575
+ ``` java
576
576
import org.junit.Before ;
577
577
import org.junit.Test ;
578
578
import com.litongjava.jfinal.aop.Aop ;
@@ -596,7 +596,7 @@ public class SysUserInfoServiceTest {
596
596
597
597
1.8.3.UserRegisterServiceTest
598
598
599
- ```
599
+ ``` java
600
600
import org.junit.Before ;
601
601
import org.junit.Test ;
602
602
import com.litongjava.tio.utils.resp.RespVo ;
@@ -625,7 +625,7 @@ public class UserRegisterServiceTest {
625
625
626
626
1.8.4.UserRegisteValidatorTest
627
627
628
- ```
628
+ ``` java
629
629
import org.junit.Test ;
630
630
631
631
import com.jfinal.kit.StrKit ;
@@ -660,7 +660,7 @@ public class UserRegisteValidatorTest {
660
660
1.9.整合 sa-token
661
661
1.9.1.SaTokenConfiguration
662
662
663
- ```
663
+ ``` java
664
664
// 导入必要的类和注解
665
665
import com.litongjava.jfinal.aop.annotation.AConfiguration ;
666
666
import com.litongjava.jfinal.aop.annotation.AInitialization ;
@@ -708,7 +708,7 @@ public class SaTokenConfiguration {
708
708
709
709
1.9.2.InterceptorConfiguration
710
710
711
- ```
711
+ ``` java
712
712
// 导入必要的类和注解
713
713
import com.litongjava.jfinal.aop.annotation.AConfiguration ;
714
714
import com.litongjava.jfinal.aop.annotation.AInitialization ;
@@ -737,7 +737,7 @@ public class InterceptorConfiguration {
737
737
1.10.登录
738
738
1.10.1.AuthController
739
739
740
- ```
740
+ ``` java
741
741
import java.util.HashMap ;
742
742
import java.util.Map ;
743
743
@@ -799,7 +799,7 @@ public class AuthController {
799
799
800
800
1.10.2.AuthValidator
801
801
802
- ```
802
+ ``` java
803
803
import com.jfinal.kit.StrKit ;
804
804
import com.litongjava.tio.boot.i18n.I18n ;
805
805
import com.litongjava.tio.boot.i18n.I18nLocale ;
@@ -831,7 +831,7 @@ public class AuthValidator {
831
831
832
832
1.10.3.AuthService
833
833
834
- ```
834
+ ``` java
835
835
import com.litongjava.jfinal.plugin.activerecord.Db ;
836
836
import com.litongjava.tio.utils.resp.RespVo ;
837
837
@@ -856,7 +856,7 @@ public class AuthService {
856
856
1.11.发送请求测试
857
857
1.11.1.注册
858
858
859
- ```
859
+ ``` shell
860
860
curl --location --request POST ' http://localhost:9204/pen-api/user/register' \
861
861
--header ' User-Agent: Apifox/1.0.0 (https://apifox.com)' \
862
862
--header ' Accept: */*' \
@@ -874,7 +874,7 @@ curl --location --request POST 'http://localhost:9204/pen-api/user/register' \
874
874
875
875
1.11.2.登录
876
876
877
- ```
877
+ ``` shell
878
878
curl --location --request POST ' http://localhost:9204/pen-api/auth/doLogin' \
879
879
--header ' User-Agent: Apifox/1.0.0 (https://apifox.com)' \
880
880
--header ' Accept: */*' \
@@ -889,15 +889,15 @@ curl --location --request POST 'http://localhost:9204/pen-api/auth/doLogin' \
889
889
响应头中保护 token
890
890
响应体如下
891
891
892
- ```
892
+ ``` json
893
893
{
894
- "ok": true
894
+ "ok" : true
895
895
}
896
896
```
897
897
898
898
1.11.3.验证 token 是否有效
899
899
900
- ```
900
+ ``` shell
901
901
curl --location --request GET ' http://localhost:9204/pen-api/auth/validateToken' \
902
902
--header ' token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
903
903
--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'
908
908
909
909
1.11.4.获取用户 id
910
910
911
- ```
911
+ ``` shell
912
912
curl --location --request GET ' http://localhost:9204/pen-api/auth/getUserId' \
913
913
--header ' token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
914
914
--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' \
917
917
--header ' Connection: keep-alive'
918
918
```
919
919
920
- ```
920
+ ``` json
921
921
{
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
928
928
}
929
929
```
930
930
931
931
1.11.5.登出
932
932
933
- ```
933
+ ``` shell
934
934
curl --location --request GET ' http://localhost:9204/pen-api/auth/logout' \
935
935
--header ' token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
936
936
--header ' User-Agent: Apifox/1.0.0 (https://apifox.com)' \
0 commit comments