springboot+vue实现发送邮箱验证码实现注册功能

前几天做一个小项目需要实现springboot集成redis实现邮箱注册功能,当时参考了某位大佬的博客来实现邮箱发送验证码的功能

http://t.csdn.cn/NO9aricon-default.png?t=M7J4http://t.csdn.cn/NO9ar

接下来就是写注册接口

controller层

 @PostMapping("/register")
    public Result Register(@RequestBody User user){
        String username=user.getUsername();
        String password=user.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password) ){
            return Result.error(Constants.CODE_400,"参数错误");
        }
        return Result.success( userService.register(user));
    }

service层

@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Result register(User user) {
        LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        lambdaQueryWrapper.eq(User::getPassword, user.getPassword());

        User one = getOne(lambdaQueryWrapper);
        if (one == null) {
            one = new User();
            BeanUtils.copyProperties(user, one);
            save(one);
        } else {
            throw new ServiceException(Constants.CODE_600, "用户已存在");
        }
        String code = redisTemplate.opsForValue().get(user.getEmail());
        if (code == null || !code.toString().equals(user.getCode())) {
            throw new ServiceException(Constants.CODE_600,"无效验证码");
        } else {
            redisTemplate.delete(code);
        }
        return Result.success(one);

    }
    }

这样后台功能就实现完成了

你可能感兴趣的:(spring,boot,后端,java)