目录
1.数据库中创建User表(username,password,salt)
2.创建User实体类(get、set方法,有无参构造,equals和hashCode方法)
3.配置数据库连接信息(application.properties)
4.在资源文件中新建mapper映射文件UserMapper.xml
5.创建mapper(dao)层新建UserMapper接口
7.重点:MD5加密-------UserServiceImpl.java
MD5加密是一种常见的加密方式,我们经常用在保存用户密码和关键信息上。
MD5技术主要用于对用户密码加密,增加账户的安全性,他具有不可逆的特性,不会被轻易解密
MD5用途:
1.防止被篡改 2.防止直接看到明文
在很多网站在数据库存储用户的密码的时候都是存储用户密码的MD5值。
1.数据库中创建User表(username,password,salt)
2.创建User实体类(get、set方法,有无参构造,equals和hashCode方法)
package com.example.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data //get,set 方法
@AllArgsConstructor //有参构造
@NoArgsConstructor //无参构造
public class User {
private String username;
private String password;
private String salt;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
if (getUsername() != null ? !getUsername().equals(user.getUsername()) : user.getUsername() != null)
return false;
if (getPassword() != null ? !getPassword().equals(user.getPassword()) : user.getPassword() != null)
return false;
return getSalt() != null ? getSalt().equals(user.getSalt()) : user.getSalt() == null;
}
@Override
public int hashCode() {
int result = getUsername() != null ? getUsername().hashCode() : 0;
result = 31 * result + (getPassword() != null ? getPassword().hashCode() : 0);
result = 31 * result + (getSalt() != null ? getSalt().hashCode() : 0);
return result;
}
}
3.配置数据库连接信息(application.properties)
spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:mapper/*.xml
#user.address.max-count=20
# 服务器向客户端不响应为null的属性
#spring.jackson.default-property-inclusion=NON_NULL
# server.servlet.context-path=/store
# spring.servlet.multipart.maxFileSize=10MB
# spring.servlet.multipart.maxRequestSize=10MB
4.在资源文件中新建mapper映射文件UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<!-- 注册-->
<insert id="addUser">
insert into user(username,password,salt) values (#{username},#{password},#{salt})
</insert>
</mapper>
5.创建mapper(dao)层新建UserMapper接口
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
void addUser(User user);
}
6.创建service层新建IUserService接口
package com.example.demo.service;
import com.example.demo.entity.User;
import org.springframework.stereotype.Service;
public interface IUserService {
void addUser(User user);
}
7.重点:MD5加密-------UserServiceImpl.java
package com.example.demo.service.Impl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
//密码加密处理的实现:md5算法的形式
//串 + password + 串 ------md5算法进行加密,连续加载三次
//盐值 + password + 盐值 --------- 盐值就是一个随机的字符串
String oldPassword = user.getPassword();
//获取盐值(随机生成一个盐值)
String salt = UUID.randomUUID().toString().toUpperCase();
//补全数据:盐值的记录
user.setSalt(salt);
//将密码和盐值作为一个整体加密处理,忽略原有密码强度,提升了数据的安全性
String md5Password = getMD5Password(oldPassword, salt);
//将加密后的密码重新补全设置到user对象中
user.setPassword(md5Password);
userMapper.addUser(user);
}
private String getMD5Password(String password,String salt){
for (int i = 0;i <3; i++){
//md5加密算法方法的调用(进行三次加密)
password = DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();
}
//返回加密后的密码
return password;
}
}
8.UserController.java
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import com.example.demo.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private IUserService iUserService;
@RequestMapping("addUser")
public String addUser(@RequestBody User user){
System.out.println(user);
iUserService.addUser(user);
return "添加成功";
}
}
9.Postman测试
发送请求