找回密码
 立即注册
查看: 34|回复: 0

springboot3对象属性拷贝和MD5加密

[复制链接]

106

主题

5

精华

110

金币

技术维护QQ:515138

积分
242
发表于 3 天前 | 显示全部楼层 |阅读模式
springboot3对象属性拷贝和MD5加密
使用代码如下:
  1. package com.zidiu.service.impl;
  2. import com.zidiu.constant.MessageConstant;
  3. import com.zidiu.constant.PasswordConstant;
  4. import com.zidiu.constant.StatusConstant;
  5. import com.zidiu.dto.EmployeeDTO;
  6. import com.zidiu.dto.EmployeeLoginDTO;
  7. import com.zidiu.entity.Employee;
  8. import com.zidiu.exception.AccountLockedException;
  9. import com.zidiu.exception.PasswordErrorException;
  10. import com.zidiu.mapper.EmployeeMapper;
  11. import com.zidiu.service.EmployeeService;
  12. import com.zidiu.utils.JwtUtils;
  13. import com.zidiu.vo.EmployeeLoginVO;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.BeanUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.util.DigestUtils;
  19. import java.time.LocalDateTime;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.Objects;
  23. @Slf4j
  24. @Service
  25. public class EmployeeServiceImpl implements EmployeeService {
  26.     @Autowired
  27.     private EmployeeMapper employeeMapper;
  28.     @Override
  29.     public EmployeeLoginVO login(EmployeeLoginDTO employeeLoginDTO) {
  30.         String username = employeeLoginDTO.getUsername();
  31.         String password = employeeLoginDTO.getPassword();
  32.         //1、根据用户名查询数据库中的数据
  33.         Employee employee = employeeMapper.getByUsername(username);
  34.         //2、处理各种异常情况(用户名不存在、密码不对、账号被锁定)
  35.         if (employee == null) {
  36.             //账号不存在
  37.             log.info("账号不存在");
  38.             throw new AccountLockedException(MessageConstant.ACCOUNT_NOT_FOUND);
  39.         }
  40.         //密码比对
  41.         // TODO 后期需要进行md5加密,然后再进行比对
  42.         if (!password.equals(employee.getPassword())) {
  43.             //密码错误
  44.             throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
  45.         }
  46.         if (Objects.equals(employee.getStatus(), StatusConstant.DISABLE)) {
  47.             //账号被锁定
  48.             throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
  49.         }
  50.         //登录成功后,生成jwt令牌
  51.         Map<String, Object> dataMap = new HashMap<>();
  52.         dataMap.put("emp_id", employee.getId());
  53.         // dataMap.put("username", employee.getUsername());
  54.         String jwt = JwtUtils.generateToken(dataMap);
  55.         // 用户ID,生成令牌
  56.         log.info("用户ID:{},生成令牌为:{}", employee.getId(), jwt);
  57.         //3、返回实体对象
  58.         return EmployeeLoginVO.builder()
  59.                 .id(employee.getId())
  60.                 .userName(employee.getUsername())
  61.                 .name(employee.getName())
  62.                 .token(jwt)
  63.                 .build();
  64.     }
  65.     /**
  66.      * 新增员工
  67.      * @param employeeDTO
  68.      */
  69.     @Override
  70.     public void save(EmployeeDTO employeeDTO) {
  71.         Employee employee = new Employee();
  72.         //对象属性拷贝
  73.         BeanUtils.copyProperties(employeeDTO, employee);
  74.         //设置账号的状态,默认正常状态 1表示正常 0表示锁定
  75.         employee.setStatus(StatusConstant.ENABLE);
  76.         //设置密码,默认密码123456
  77.         employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
  78.         //设置当前记录的创建时间和修改时间
  79.         employee.setCreateTime(LocalDateTime.now());
  80.         employee.setUpdateTime(LocalDateTime.now());
  81.         //设置当前记录创建人id和修改人id
  82.         employee.setCreateUser(10L);//目前写个假数据,后期修改
  83.         employee.setUpdateUser(10L);
  84.         employeeMapper.insert(employee);//后续步骤定义
  85.     }
  86. }
复制代码

        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());

SpringBoot3+SSM的企业级Java项目实战之外卖小程序


上一篇:springboot3+Swagger文档knife4j依赖
下一篇:springboot3操作时间显示问题2025-12-18T14:35:36
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2025-12-21 16:49

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表