当前位置:网站首页>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 ;
边栏推荐
猜你喜欢

Enterprise middle office planning and it architecture microservice transformation

KubeVela 1.4:让应用交付更安全、上手更简单、过程更透明

MySQL master-slave synchronization

如何快速通过PMP考试?

CADD course learning (2) -- target crystal structure information

Exness: liquidity series - liquidity cleaning and reversal, decision interval

更智能!AIRIOT加速煤炭行业节能减排升级
![[try to hack] windows system account security](/img/2b/e6e999313e3ae4e1cbf4bfa02daef0.png)
[try to hack] windows system account security

Tencent conference application market was officially launched, with more than 20 applications in the first batch

Unity 如何拖拉多个组件中的一个
随机推荐
Advanced skills of testers: a guide to the application of unit test reports
Enterprise middle office planning and it architecture microservice transformation
盘点华为云GaussDB(for Redis)六大秒级能力
Conditional compilation
Exness: liquidity series - liquidity cleaning and reversal, decision interval
Why must we move from Devops to bizdevops?
【1175. 质数排列】
Which brokerage has the lowest commission? In addition, is it safe to open a mobile account?
QT :QAxObject操作Excel
数据智能——DTCC2022!中国数据库技术大会即将开幕
pytorch实现FLOPs和Params的计算
DNS服务器搭建、转发、主从配置
条件编译
【450. 删除二叉搜索树中的节点】
闲鱼难“翻身”
《微信小程序-基础篇》带你了解小程序中的生命周期(二)
Solution to rollback of MySQL database by mistake deletion
4.3-inch touch screen 12 channel control port programmable network central control supports mutual backup of 5 central control hosts
PostgreSQL heap堆表 存储引擎实现原理
exness:美GDP终值意外加速萎缩1.6%













