当前位置:网站首页>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 ;

2. stay 【imooc-news-dev-service-user】 User microservices PassportController Class , Realization 【 register / Sign in 】 Interface ;

3. effect ;


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 ;

原网站

版权声明
本文为[Small withered forest]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/181/202206301933118422.html

随机推荐