当前位置:   article > 正文

SpringBoot+MyBatis实现MD5加密数据库用户密码_md5password

md5password

目录

1.数据库中创建User表(username,password,salt)

2.创建User实体类(get、set方法,有无参构造,equals和hashCode方法)

3.配置数据库连接信息(application.properties)

4.在资源文件中新建mapper映射文件UserMapper.xml

5.创建mapper(dao)层新建UserMapper接口

6.创建service层新建IUserService接口

7.重点:MD5加密-------UserServiceImpl.java                                                

8.UserController.java

9.Postman测试


MD5加密是一种常见的加密方式,我们经常用在保存用户密码和关键信息上。

MD5技术主要用于对用户密码加密,增加账户的安全性,他具有不可逆的特性,不会被轻易解密

MD5用途:
1.防止被篡改                        2.防止直接看到明文

在很多网站在数据库存储用户的密码的时候都是存储用户密码的MD5值。

1.数据库中创建User表(username,password,salt

2.创建User实体类(get、set方法,有无参构造,equals和hashCode方法)

  1. package com.example.demo.entity;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data //get,set 方法
  6. @AllArgsConstructor //有参构造
  7. @NoArgsConstructor //无参构造
  8. public class User {
  9. private String username;
  10. private String password;
  11. private String salt;
  12. @Override
  13. public boolean equals(Object o) {
  14. if (this == o) return true;
  15. if (!(o instanceof User)) return false;
  16. User user = (User) o;
  17. if (getUsername() != null ? !getUsername().equals(user.getUsername()) : user.getUsername() != null)
  18. return false;
  19. if (getPassword() != null ? !getPassword().equals(user.getPassword()) : user.getPassword() != null)
  20. return false;
  21. return getSalt() != null ? getSalt().equals(user.getSalt()) : user.getSalt() == null;
  22. }
  23. @Override
  24. public int hashCode() {
  25. int result = getUsername() != null ? getUsername().hashCode() : 0;
  26. result = 31 * result + (getPassword() != null ? getPassword().hashCode() : 0);
  27. result = 31 * result + (getSalt() != null ? getSalt().hashCode() : 0);
  28. return result;
  29. }
  30. }

3.配置数据库连接信息(application.properties)

  1. spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
  2. spring.datasource.username=root
  3. spring.datasource.password=123456
  4. mybatis.mapper-locations=classpath:mapper/*.xml
  5. #user.address.max-count=20
  6. # 服务器向客户端不响应为null的属性
  7. #spring.jackson.default-property-inclusion=NON_NULL
  8. # server.servlet.context-path=/store
  9. # spring.servlet.multipart.maxFileSize=10MB
  10. # spring.servlet.multipart.maxRequestSize=10MB

4.在资源文件中新建mapper映射文件UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.demo.mapper.UserMapper">
  4. <!-- 注册-->
  5. <insert id="addUser">
  6. insert into user(username,password,salt) values (#{username},#{password},#{salt})
  7. </insert>
  8. </mapper>

5.创建mapper(dao)层新建UserMapper接口

  1. package com.example.demo.mapper;
  2. import com.example.demo.entity.User;
  3. import org.apache.ibatis.annotations.Mapper;
  4. @Mapper
  5. public interface UserMapper {
  6. void addUser(User user);
  7. }

6.创建service层新建IUserService接口

  1. package com.example.demo.service;
  2. import com.example.demo.entity.User;
  3. import org.springframework.stereotype.Service;
  4. public interface IUserService {
  5. void addUser(User user);
  6. }

7.重点:MD5加密-------UserServiceImpl.java

  1. package com.example.demo.service.Impl;
  2. import com.example.demo.entity.User;
  3. import com.example.demo.mapper.UserMapper;
  4. import com.example.demo.service.IUserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.util.DigestUtils;
  8. import java.util.Date;
  9. import java.util.Locale;
  10. import java.util.UUID;
  11. @Service
  12. public class UserServiceImpl implements IUserService {
  13. @Autowired
  14. private UserMapper userMapper;
  15. @Override
  16. public void addUser(User user) {
  17. //密码加密处理的实现:md5算法的形式
  18. //串 + password + 串 ------md5算法进行加密,连续加载三次
  19. //盐值 + password + 盐值 --------- 盐值就是一个随机的字符串
  20. String oldPassword = user.getPassword();
  21. //获取盐值(随机生成一个盐值)
  22. String salt = UUID.randomUUID().toString().toUpperCase();
  23. //补全数据:盐值的记录
  24. user.setSalt(salt);
  25. //将密码和盐值作为一个整体加密处理,忽略原有密码强度,提升了数据的安全性
  26. String md5Password = getMD5Password(oldPassword, salt);
  27. //将加密后的密码重新补全设置到user对象中
  28. user.setPassword(md5Password);
  29. userMapper.addUser(user);
  30. }
  31. private String getMD5Password(String password,String salt){
  32. for (int i = 0;i <3; i++){
  33. //md5加密算法方法的调用(进行三次加密)
  34. password = DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();
  35. }
  36. //返回加密后的密码
  37. return password;
  38. }
  39. }

8.UserController.java

  1. package com.example.demo.controller;
  2. import com.example.demo.entity.User;
  3. import com.example.demo.service.IUserService;
  4. import com.example.demo.service.Impl.UserServiceImpl;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. public class UserController {
  11. @Autowired
  12. private IUserService iUserService;
  13. @RequestMapping("addUser")
  14. public String addUser(@RequestBody User user){
  15. System.out.println(user);
  16. iUserService.addUser(user);
  17. return "添加成功";
  18. }
  19. }

9.Postman测试

 发送请求

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/875374
推荐阅读
相关标签
  

闽ICP备14008679号