邮箱注册实现(二)注册接口实现

如果邮箱地址错误或非法,运行时会报错。因此需要增加校验:

@Validated
@RestController
@RequestMapping("/api/auth")
public class AuthorizeController {
    @Resource
    AccountService service;

    @GetMapping("/ask-code")
    public RestBean askVerifyCode(@RequestParam @Email String email,
                                        @RequestParam @Pattern(regexp = "(register|reset)") String type,
                                        HttpServletRequest request) {
        System.out.println("发送邮箱为:"+email);
        String message = service.RegisterEmailVerifyCode(type, email, request.getRemoteAddr());
        return message== null ?  RestBean.success() : RestBean.failure(400, message) ;
    }

}

此时可以对非法邮箱地址进行拦截,但返回的错误信息仍然是401,新建ValidationController进行报错的统一,对应地址为/error的控制器,记得要在安全设置中放行。

@Slf4j
@RestControllerAdvice
public class ValidationController {

    @ExceptionHandler({ValidationException.class})
    public RestBean validationException(ValidationException exception) {
        log.warn("Resolve[{}:{}]",exception.getClass().getName(),exception.getMessage());
        return RestBean.failure(400, "请求参数有误");
    }

}

邮箱注册实现(二)注册接口实现_第1张图片

在service中增加注册接口String registerEmailAccount(EmailRegisterVO);

由于用户注册账号时需要提交一个表单,包括用户名密码等信息,因此注册一个实体类,EmailRegisterVO

@Data
public class EmailRegisterVO {
    @Email
    String email;
    @Length(max=6,min=6)
    String code;
    @Pattern(regexp = "^[a-zA-Z0-9_]{3,16}$")
            @Length(min =1,max=10)
    String username;
    @Length(min=6,max=20)
    String password;
}

需要判断用户名邮箱有没有冲突,在数据库中添加索引,关联字段为username和email

 @Override
    public String registerEmailAccount(EmailRegisterVO vo) {
        String email=vo.getEmail();
        String username=vo.getUsername();
        String code=template.opsForValue().get(Const.VERIFY_EMAIL_DATA+email);
        if (code==null){
            return "请先获取验证码";
        }
        if (!code.equals(vo.getCode())){
            return "验证码错误";
        }
        if (this.existsAccountByEmail(email)){
            return "此电子邮件已被其他用户注册";
        }
        if (this.existsAccountByUsername(vo.getUsername())){
            return "此用户名已被其他用户注册,请更新一个新的用户名";
        }
        String password= encoder.encode(vo.getPassword());
        Account account=new Account(null,username,password,email,"user",new Date());
        if (this.save(account)){
            template.delete(Const.VERIFY_EMAIL_DATA+email);
            return null;
        }else {
            return "内部错误";
        }
    }

    private boolean existsAccountByEmail(String email){
        return this.baseMapper.exists(Wrappers.query().eq("email",email));
    }

    private boolean existsAccountByUsername(String username){
        return this.baseMapper.exists(Wrappers.query().eq("username",username));
    }

编写controller:

    @GetMapping("/ask-code")
    public RestBean askVerifyCode(@RequestParam @Email String email,
                                        @RequestParam @Pattern(regexp = "(register|reset)") String type,
                                        HttpServletRequest request) {
        System.out.println("发送邮箱为:"+email);
        return this.messageHandle(() -> service.RegisterEmailVerifyCode(type, email, request.getRemoteAddr()));
    }

    @PostMapping("/register")
    public RestBean register(@RequestBody @Valid EmailRegisterVO vo) {
        return this.messageHandle(()->service.registerEmailAccount(vo));
    }
    private  RestBean messageHandle(Supplier  action){
        String message=action.get();
        return message==null ?  RestBean.success() : RestBean.failure(400, message) ;
    }

邮箱注册实现(二)注册接口实现_第2张图片

获取验证码后请求成功

邮箱注册实现(二)注册接口实现_第3张图片

数据库中增加了相关的用户

邮箱注册实现(二)注册接口实现_第4张图片

你可能感兴趣的:(java)