1
- tio-boot-生产级别案例-用户登录和注册
2
- 本章节使用tio-boot开发一个用户注册和登录系统
3
- 首先会在mysql中创建一张表
4
- 然后使用jfinal-plugins连接mysql
5
- 使用sa-token将用户信息保存到reids
1
+ # tio-boot-案例-用户登录和注册
2
+
3
+ 本章节使用 tio-boot 开发一个用户注册和登录系统
4
+ 首先会在 mysql 中创建一张表
5
+ 然后使用 jfinal-plugins 连接 mysql
6
+ 使用 sa-token 将用户信息保存到 reids
6
7
1.1.创建表
8
+
7
9
```
8
10
CREATE TABLE IF NOT EXISTS `sys_user_info` (
9
11
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
@@ -130,15 +132,19 @@ CREATE TABLE IF NOT EXISTS `sys_user_info` (
130
132
</dependency>
131
133
</dependencies>
132
134
```
135
+
133
136
1.3.配置文件
134
137
app.properties
138
+
135
139
```
136
140
server.port=9204
137
141
server.context-path=/pen-api
138
142
# 或 prod
139
143
app.env=dev
140
144
```
145
+
141
146
app-dev.properties
147
+
142
148
```
143
149
jdbc.url=jdbc:mysql://192.168.3.9/enote?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
144
150
jdbc.user=root
@@ -153,6 +159,7 @@ redis.timeout=15000
153
159
```
154
160
155
161
i18n_en_US.properties
162
+
156
163
```
157
164
The_username_or_password_cannot_be_empty=The username or password cannot be empty
158
165
The_locale_cannot_be_empty=The locale cannot be empty
@@ -166,7 +173,9 @@ The_password_should_contain_uppercase_letters,_lowercase_letters_and_numbers=The
166
173
The_locale_at_least_5_characters=The locale at least 5 characters
167
174
The_username_exists=The username exists
168
175
```
176
+
169
177
i18n_zh_CN.properties
178
+
170
179
```
171
180
The_username_or_password_cannot_be_empty=用户名或者密码不能为空
172
181
The_locale_cannot_be_empty=locale不能为空
@@ -180,7 +189,9 @@ The_password_should_contain_uppercase_letters,_lowercase_letters_and_numbers=pas
180
189
The_locale_at_least_5_characters=locale至少5个字符
181
190
The_username_exists=用户名已经存在
182
191
```
192
+
183
193
1.4.启动类
194
+
184
195
```
185
196
import com.litongjava.hotswap.wrapper.tio.boot.TioApplicationWrapper;
186
197
import com.litongjava.jfinal.aop.annotation.AComponentScan;
@@ -189,7 +200,7 @@ import com.litongjava.jfinal.aop.annotation.AComponentScan;
189
200
public class MaLiangPenAiServerApp {
190
201
public static void main(String[] args) {
191
202
long start = System.currentTimeMillis();
192
-
203
+
193
204
TioApplicationWrapper.run(MaLiangPenAiServerApp.class, args);
194
205
//TioApplication.run(MaLiangPenAiServerApp.class, args);
195
206
long end = System.currentTimeMillis();
@@ -198,7 +209,8 @@ public class MaLiangPenAiServerApp {
198
209
}
199
210
```
200
211
201
- 1.5.连接mysql
212
+ 1.5.连接 mysql
213
+
202
214
```
203
215
package com.enoleap.manglang.pen.api.server.config;
204
216
@@ -242,7 +254,7 @@ public class TableToJsonConfig {
242
254
}
243
255
244
256
/*
245
- *
257
+ *
246
258
* config ActiveRecordPlugin
247
259
*/
248
260
@AInitialization
@@ -252,12 +264,12 @@ public class TableToJsonConfig {
252
264
253
265
ActiveRecordPlugin arp = new ActiveRecordPlugin(dataSource);
254
266
arp.setContainerFactory(new OrderedFieldContainerFactory());
255
-
267
+
256
268
Engine engine = arp.getEngine();
257
269
engine.setSourceFactory(new ClassPathSourceFactory());
258
270
engine.setCompressorOn(' ');
259
271
engine.setCompressorOn('\n');
260
-
272
+
261
273
if ("dev".equals(property)) {
262
274
arp.setDevMode(true);
263
275
engine.setDevMode(true);
@@ -270,7 +282,9 @@ public class TableToJsonConfig {
270
282
}
271
283
}
272
284
```
273
- 1.6.连接reids
285
+
286
+ 1.6.连接 reids
287
+
274
288
```
275
289
import com.litongjava.jfinal.aop.annotation.AConfiguration;
276
290
import com.litongjava.jfinal.aop.annotation.AInitialization;
@@ -304,10 +318,12 @@ public class RedisPluginConfig {
304
318
}
305
319
}
306
320
```
321
+
307
322
1.7.注册
308
323
注册的业务逻辑是获取请求信息,验证请求信息,验证通过插入到数据库
309
324
310
325
1.7.1.UserRegisterVO
326
+
311
327
```
312
328
import lombok.AllArgsConstructor;
313
329
import lombok.Builder;
@@ -332,7 +348,9 @@ public class UserRegisterVO {
332
348
333
349
}
334
350
```
351
+
335
352
1.7.2.UserRegisterController
353
+
336
354
```
337
355
import com.litongjava.jfinal.aop.annotation.AAutowired;
338
356
import com.litongjava.tio.http.server.annotation.RequestPath;
@@ -345,20 +363,22 @@ public class UserRegisterController {
345
363
private UserRegisteValidator userRegisteValidator;
346
364
@AAutowired
347
365
private UserRegisterService userRegisterService;
348
-
366
+
349
367
@RequestPath
350
368
public RespVo index(UserRegisterVO reqVo) {
351
369
RespVo respVo = userRegisteValidator.index(reqVo);
352
370
if (respVo != null) {
353
371
return respVo;
354
372
}
355
-
373
+
356
374
return userRegisterService.index(reqVo);
357
375
}
358
376
}
359
377
360
378
```
379
+
361
380
1.7.3.UserRegisteValidator
381
+
362
382
```
363
383
package com.enoleap.manglang.pen.api.server.validator;
364
384
@@ -372,7 +392,7 @@ import com.litongjava.tio.boot.i18n.Res;
372
392
import com.litongjava.tio.utils.resp.RespVo;
373
393
374
394
public class UserRegisteValidator {
375
-
395
+
376
396
@AAutowired
377
397
private SysUserInfoService sysUserInfoService;
378
398
@@ -445,7 +465,9 @@ public class UserRegisteValidator {
445
465
}
446
466
}
447
467
```
468
+
448
469
1.7.4.SysUserInfoService
470
+
449
471
```
450
472
import com.litongjava.jfinal.plugin.activerecord.Db;
451
473
@@ -469,6 +491,7 @@ public class SysUserInfoService {
469
491
```
470
492
471
493
1.7.5.UserRegisterService
494
+
472
495
```
473
496
import com.litongjava.jfinal.plugin.activerecord.Db;
474
497
import com.litongjava.tio.utils.resp.RespVo;
@@ -520,6 +543,7 @@ public class UserRegisterService {
520
543
521
544
1.8.单元测试注册
522
545
1.8.1.TioBootTest
546
+
523
547
```
524
548
import java.util.List;
525
549
@@ -545,7 +569,9 @@ public class TioBootTest {
545
569
}
546
570
}
547
571
```
572
+
548
573
1.8.2.SysUserInfoServiceTest
574
+
549
575
```
550
576
import org.junit.Before;
551
577
import org.junit.Test;
@@ -567,7 +593,9 @@ public class SysUserInfoServiceTest {
567
593
568
594
}
569
595
```
596
+
570
597
1.8.3.UserRegisterServiceTest
598
+
571
599
```
572
600
import org.junit.Before;
573
601
import org.junit.Test;
@@ -596,6 +624,7 @@ public class UserRegisterServiceTest {
596
624
```
597
625
598
626
1.8.4.UserRegisteValidatorTest
627
+
599
628
```
600
629
import org.junit.Test;
601
630
@@ -628,8 +657,9 @@ public class UserRegisteValidatorTest {
628
657
}
629
658
```
630
659
631
- 1.9.整合sa -token
660
+ 1.9.整合 sa -token
632
661
1.9.1.SaTokenConfiguration
662
+
633
663
```
634
664
// 导入必要的类和注解
635
665
import com.litongjava.jfinal.aop.annotation.AConfiguration;
@@ -675,7 +705,9 @@ public class SaTokenConfiguration {
675
705
}
676
706
}
677
707
```
678
- 1.9.2.InterceptorConfiguration
708
+
709
+ 1.9.2.InterceptorConfiguration
710
+
679
711
```
680
712
// 导入必要的类和注解
681
713
import com.litongjava.jfinal.aop.annotation.AConfiguration;
@@ -701,8 +733,10 @@ public class InterceptorConfiguration {
701
733
}
702
734
}
703
735
```
736
+
704
737
1.10.登录
705
738
1.10.1.AuthController
739
+
706
740
```
707
741
import java.util.HashMap;
708
742
import java.util.Map;
@@ -762,7 +796,9 @@ public class AuthController {
762
796
}
763
797
}
764
798
```
799
+
765
800
1.10.2.AuthValidator
801
+
766
802
```
767
803
import com.jfinal.kit.StrKit;
768
804
import com.litongjava.tio.boot.i18n.I18n;
@@ -779,11 +815,11 @@ public class AuthValidator {
779
815
} else {
780
816
res = I18n.use(I18nLocale.EN_US);
781
817
}
782
-
818
+
783
819
if (StrKit.isBlank(locale)) {
784
820
return RespVo.fail(res.get("The_locale_cannot_be_empty"));
785
821
}
786
-
822
+
787
823
if(StrKit.isBlank(username) || StrKit.isBlank(password)) {
788
824
String string = res.get("The_username_or_password_cannot_be_empty");
789
825
return RespVo.fail(string);
@@ -792,7 +828,9 @@ public class AuthValidator {
792
828
}
793
829
}
794
830
```
831
+
795
832
1.10.3.AuthService
833
+
796
834
```
797
835
import com.litongjava.jfinal.plugin.activerecord.Db;
798
836
import com.litongjava.tio.utils.resp.RespVo;
@@ -817,6 +855,7 @@ public class AuthService {
817
855
818
856
1.11.发送请求测试
819
857
1.11.1.注册
858
+
820
859
```
821
860
curl --location --request POST 'http://localhost:9204/pen-api/user/register' \
822
861
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
@@ -834,6 +873,7 @@ curl --location --request POST 'http://localhost:9204/pen-api/user/register' \
834
873
```
835
874
836
875
1.11.2.登录
876
+
837
877
```
838
878
curl --location --request POST 'http://localhost:9204/pen-api/auth/doLogin' \
839
879
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
@@ -846,14 +886,17 @@ curl --location --request POST 'http://localhost:9204/pen-api/auth/doLogin' \
846
886
--data-urlencode 'locale=zh_CN'
847
887
```
848
888
849
- 响应头中保护token
889
+ 响应头中保护 token
850
890
响应体如下
891
+
851
892
```
852
893
{
853
894
"ok": true
854
895
}
855
896
```
856
- 1.11.3.验证token是否有效
897
+
898
+ 1.11.3.验证 token 是否有效
899
+
857
900
```
858
901
curl --location --request GET 'http://localhost:9204/pen-api/auth/validateToken' \
859
902
--header 'token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
@@ -863,14 +906,15 @@ curl --location --request GET 'http://localhost:9204/pen-api/auth/validateToken'
863
906
--header 'Connection: keep-alive' \
864
907
```
865
908
866
- 1.11.4.获取用户id
909
+ 1.11.4.获取用户 id
910
+
867
911
```
868
912
curl --location --request GET 'http://localhost:9204/pen-api/auth/getUserId' \
869
913
--header 'token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
870
914
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
871
915
--header 'Accept: */*' \
872
916
--header 'Host: localhost:9204' \
873
- --header 'Connection: keep-alive'
917
+ --header 'Connection: keep-alive'
874
918
```
875
919
876
920
```
@@ -883,7 +927,9 @@ curl --location --request GET 'http://localhost:9204/pen-api/auth/getUserId' \
883
927
"ok": true
884
928
}
885
929
```
930
+
886
931
1.11.5.登出
932
+
887
933
```
888
934
curl --location --request GET 'http://localhost:9204/pen-api/auth/logout' \
889
935
--header 'token: 4eb6ac726a7d4f42bb10fe365823f9f7' \
@@ -892,4 +938,3 @@ curl --location --request GET 'http://localhost:9204/pen-api/auth/logout' \
892
938
--header 'Host: localhost:9204' \
893
939
--header 'Connection: keep-alive'
894
940
```
895
-
0 commit comments