当前位置:网站首页>25: Chapter 3: developing pass service: 8: [registration / login] interface: receiving and verifying "mobile number and verification code" parameters; (it is important to know the application scenario
25: Chapter 3: developing pass service: 8: [registration / login] interface: receiving and verifying "mobile number and verification code" parameters; (it is important to know the application scenario
2022-06-30 20:10:00 【Small withered forest】
explain :
(1) The content of this blog : Start developing 【 One click registration / Sign in 】 Interface ;
● then , The development part of this blog is : The user is clicking 【 One click registration / Sign in 】 After button , The back end will verify 【 Whether the user has entered the mobile phone number or verification code 】、【 Whether the verification code entered by the user matches 】;
(2) The points needing attention in this blog :
● Created BO Entity class , To accept the data submitted by the front-end form ;
● Used 【@Valid annotation 】 Parameter checking ;
● Some common methods , Can be defined in 【imooc-news-dev-service-api】 Interface Engineering BaseController Class ;
● Pay attention to the overall process of parameter verification ;
Catalog
1. stay 【imooc-news-dev-service-api】 Interface Engineering PassportControllerApi Interface , Definition 【 register / Sign in 】 Interface ;
/** * 【 One click registration / Sign in , Interface 】 * @param registLoginBo * @param result * @return */ @ApiOperation(value = " One click registration / Login interface ", notes = " One click registration / Login interface ", httpMethod = "POST") // The request interface on the front end is already “getSMSCode” 了 , So I am writing back-end interface url When , Don't write ; @PostMapping("/doLogin") public GraceJSONResult doLogin(@RequestBody @Valid RegistLoginBo registLoginBo, BindingResult result);explain :
(1) The interface request mode is POST, Because the front end is actually a form , The corresponding form submission generally uses POST The way ;
(2) Generally speaking , When receiving form data , We can use objects to take parameters ;
● So we are 【imooc-news-dev-model】 In model engineering , establish bo package , establish RegistLoginBo Entity class ;(BO It means , Objects passed from the page view layer , It can also be understood as Bussiness Object)( In this project ,BO Entity classes are passed from the front end to the back end ,VO Entity classes are passed from the back end to the front end )
package com.imooc.bo; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; /** * BO Entity class : Undertaking 【 One click registration / Sign in 】 Interface parameters ; */ public class RegistLoginBo { @NotBlank(message = " Cell phone number cannot be empty ") private String mobile; @NotBlank(message = " SMS verification code cannot be empty ") private String smsCode; public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getSmsCode() { return smsCode; } public void setSmsCode(String smsCode) { this.smsCode = smsCode; } @Override public String toString() { return "RegistLoginBo{" + "mobile='" + mobile + '\'' + ", smsCode='" + smsCode + '\'' + '}'; } }● Here we use 【@Valid annotation 】 Parameter checking ; You can refer to 【Spring Boot E-commerce projects 24: Commodity classification module III : Use 【@Valid annotation 】 Check the input parameters ;】;
● among 【@NotNull】 annotation , Only the completely empty case can be verified , If the front-end input a value such as 【" "】 Empty string for , This annotation cannot be verified ;;; and 【@NotBlank】 The annotation can not only check the case that it is completely empty , It can also be verified as 【" "】 Empty string for ;
● then , Remember in the interface RegistLoginBo There are parameters , To use 【@Valid】 annotation , To enable parameter verification ;
● If we use... On entity classes lombok After the comments , You can omit get and set Such method ;;; however , Many companies do not recommend using lombok Of ( Because when it is combined with some third-party libraries , There may be some small bug)
● then , If we use bean When receiving parameters , Need to use 【@RequestBody】 annotation ;;; The main meaning of this note is 【 From the front JSON】 and 【 hinder RegistLoginBo registLoginBo】 Is the corresponding , In order to accept parameters ;
● Once the verification fails , There is an error message , We can go through 【BindingResult result】 To get the corresponding error information ; therefore , Here in the parameter , It has also been introduced. 【BindingResult result】;
● The content here , I've met before N Time , It's not particularly complicated , It's just a little more ;
2. stay 【imooc-news-dev-service-user】 User microservices PassportController Class , Realization 【 register / Sign in 】 Interface ;
/** * 【 One click registration / Sign in , Interface 】 * @param registLoginBo * @param result * @return */ @Override public GraceJSONResult doLogin(@Valid RegistLoginBo registLoginBo, BindingResult result) { //0. Judge BindingResult Whether the error message of validation failure is saved in , If there is , It indicates that there is a problem with the input of the front end ( Mobile phone number or verification code , At least one has not been entered ); // that , We get this error message , And build a GraceJSONResult Unified return object , return ; if (result.hasErrors()) { Map<String, String> map = getErrorsFromBindingResult(result); return GraceJSONResult.errorMap(map); } //1. Check whether the verification code matches ; //1.1 Get the mobile phone number and verification code entered by the user in the front end ; String mobile = registLoginBo.getMobile(); String smsCode = registLoginBo.getSmsCode(); //1.2 According to the mobile phone number entered by the user in the front end , Try to go redis Get the corresponding verification code in ; String redisSMSCode = redisOperator.get(MOBILE_SMSCODE + ":" + mobile); //1.3 If the verification code entered by the front end , stay redis Does not exist in the ( explain : We are not targeting 【 The mobile phone number entered by the user 】 Sent verification code ; // Or after the user receives the verification code text message , After that 30min Before use ,redis The verification code stored in the has expired ), // Or the front-end input verification code and redis The difference between ( explain : The user's verification code is entered incorrectly ); // that , Go back to the corresponding , With error messages GraceJSONResult Unified return object ; // among , Here we use 【org.apache.commons.lang3】 Medium StringUtils The utility class isBlank() Method to judge the null ; if (StringUtils.isBlank(redisSMSCode) || !redisSMSCode.equals(smsCode)) { return GraceJSONResult.errorCustom(ResponseStatusEnum.SMS_CODE_ERROR); } return GraceJSONResult.ok(); }explain :
(1) Judge 【 Parameter checking 】 whether OK;
● We are 【imooc-news-dev-service-api】 Interface Engineering BaseController in , Defines a method getErrorsFromBindingResult(): from BindingResult Extract error information from ;( This method , Later in other places , It will also be used )
(2) Check whether the verification code matches ;
● explain 1;
● explain 2;
3. effect ;
(1) overall situation install Look at the whole project ;
(2) start-up 【imooc-news-dev-service-usr】 Main startup class of user microservice ;
(3) We are 【http://localhost:8003/doc.html】Swagger2 On the page , To test the interface ;
● Failure situation ;
● OK The situation of ;
边栏推荐
猜你喜欢

Summary of operating system interview questions (updated from time to time)

Unity 如何拖拉多个组件中的一个
Redis ziplist 压缩列表的源码解析

How unity pulls one of multiple components

Character class of regular series

“更福特、更中国”拨云见日,长安福特王牌产品订单过万
![[try to hack] windows system account security](/img/2b/e6e999313e3ae4e1cbf4bfa02daef0.png)
[try to hack] windows system account security
![[solved] how does Tiktok cancel paying attention to the cancelled account](/img/1f/7b0bd2c0f69f7f3d1c25c426cc5771.png)
[solved] how does Tiktok cancel paying attention to the cancelled account

屏幕显示技术进化史

S7-1500 PLC之间进行TCP通信的具体方法和步骤详解(图文)
随机推荐
S7-1500 PLC之间进行TCP通信的具体方法和步骤详解(图文)
Taiwan SSS Xinchuang sss1700 replaces cmedia cm6533 24bit 96KHz USB audio codec chip
NLP技能树学习路线-(一)路线总览
Data intelligence - dtcc2022! China database technology conference is about to open
FH6908A负极关断同步整流模拟低压降二极管控制IC芯片TSOT23-6超低功耗整流器 1w功耗 <100uA静态 替代MP6908
MySQL master-slave synchronization
25:第三章:开发通行证服务:8:【注册/登录】接口:接收并校验“手机号和验证码”参数;(重点需要知道【利用redis来暂存数据,获取数据的】的应用场景)(使用到了【@Valid注解】参数校验)
Advanced skills of testers: a guide to the application of unit test reports
项目经理是领导吗?可以批评指责成员吗?
一文读懂目标检测:R-CNN、Fast R-CNN、Faster R-CNN、YOLO、SSD「建议收藏」
CADD course learning (1) -- basic knowledge of drug design
MySQL billing Statistics (Part 1): MySQL installation and client dbeaver connection
Is it safe to open an account in Guangzhou stock exchange by mobile phone?
Enterprise middle office planning and it architecture microservice transformation
Primary school, session 3 - afternoon: Web_ xxe
计网 | 【五 传输层、六 应用层】知识点及例题
mysql统计账单信息(上):mysql安装及客户端DBeaver连接使用
Lombok
网易云签到可抽奖?那一年我能签到365天。不信?你看。
微信小程序开发实战 云音乐













