• 伙伴匹配(后端)-- 用户注册功能


    注册逻辑设计


    注意:对密码进行加密(密码千万不要直接以明文存储在数据库中)

    注册代码实现

    package com.example.usercenterbackend.service.impl;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.example.usercenterbackend.service.UserService;
    import com.example.usercenterbackend.modal.domain.User;
    import com.example.usercenterbackend.Mapper.UserMapper;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.util.DigestUtils;
    
    import javax.annotation.Resource;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * @author 13425
     * @description 针对表【user(用户)】的数据库操作Service实现
     * @createDate 2024-03-06 19:21:19
     */
    @Service
    public class UserServiceImpl extends ServiceImpl<UserMapper, User>
            implements UserService {
    
        @Resource
        private UserMapper userMapper;
    
        @Override
        public Long userRegister(String userAccount, String userPassword, String checkPassword) {
            //1.账户,密码,校验码为空
            if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
                return null;
            }
            // 2.账户小于4位
            if (userAccount.length() < 4) {
                return null;
            }
            // 3.密码,校验码小于8位
            if (userPassword.length() < 8 || checkPassword.length() < 8) {
                return null;
            }
            // 4.账户包含特殊字符(正则表达式)
            String validPattern = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
            Matcher matcher = Pattern.compile(validPattern).matcher(userAccount);
            if (matcher.find()) {
                return null;
            }
            // 5.密码和校验码不同
            if (!userPassword.equals(checkPassword)) {
                return null;
            }
            // 6.账户重复,放在后面,可以节省查询次数,节省内存性能
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userAccount", userAccount);
            Long count = userMapper.selectCount(queryWrapper);
            if (count > 0) {
                return null;
            }
            //校验完成后,加密
            String SALT = "yupi";
            String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
            //注册成功,插入数据到数据库
            User user = new User();
            user.setUserAccount(userAccount);
            user.setUserPassword(encryptPassword);
            boolean result = this.save(user);
            if (!result) {
                return null;
            }
            return user.getId();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    简化开发

    SringUtils简化开发
    需要导入下面这个包

    <dependency>
      <groupId>org.apache.commonsgroupId>
      <artifactId>commons-lang3artifactId>
      <version>3.12.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Debug快捷键

    f7进入此方法 f9下一个断点 f8下一步

  • 相关阅读:
    Springboot - 3.BeanFactory
    arm LINUX 启动时间的一些优化项汇总
    丁鹿学堂:前端面试手写系列之promise(一)
    鲁棒优化不确定性问题
    408 考研《操作系统》第一章第一节:操作系统的概念和特征
    SSL证书验证失败
    2022年12月 Python(一级)真题解析#中国电子学会#全国青少年软件编程等级考试
    循环神经网络
    Layui之新增修改功能
    【计算机网络】网络模型
  • 原文地址:https://blog.csdn.net/m0_74870396/article/details/138217367