当前位置:网站首页>【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
2022-08-04 03:08:00 【#小苏打】
MD5的pom
<!-- 专门做MD5的加密包-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>需求分析
- 注册用户时,注册的密码在数据库中进行加密
- 登入用户时,采用MD5验证后密码正确登录用户
需求解决
- 数据库建表时,在user表中设置一个slat列,每一次注册用户时,生成一个独一无二的slat(盐)写入数据库slat列,并使用md5+slat的方式进行密码加密,写入数据库password列。
- 登录用户时,先根据userid获得slat,输入密码和md5+slat加密之后得到的password和数据库password对比,相同,则登入成功。
注册用户代码
1.生成slat的工具类(自己随便写的,保证生成的数字不一样就行)
public class SlatUtile {
public static Integer createSlat(){
int i = new Random().nextInt();
return i;
}
}2.注册用户mapper
@Repository
public interface AddUserMapper {
@Insert("insert into user(userid,username,phonenumber,password,slat)value(#{userid},#{username},#{phonenumber},#{password},#{slat})")
Integer addUser(Integer userid, String username,String phonenumber,String password,Integer slat);
}3.注册用户service
//service接口
public interface AddUserService {
Integer addUser(Integer userid, String username,String phonenumber,String password,Integer slat);
}@Service
public class AddUserServiceImp implements AddUserService {
@Autowired
private AddUserMapper addUserMapper;
//注册新用户,密码进行采用MD5+盐加密
@Override
public Integer addUser(Integer userid,String username,String phonenumber,String password,Integer slat) {
Integer slatforthis = SlatUtile.createSlat(); //每次用户注册新生成一个salt
String md5password = DigestUtils.md5Hex(password+slatforthis); //对密码进行加密
Integer integer = addUserMapper.addUser(userid, username, phonenumber, md5password,slatforthis);
return integer;
}
}4.注册用户controller
@RestController
public class AddUserController {
@Autowired
private AddUserService addUserService;
@RequestMapping("adduser")
public String adduser(Integer userid, String username, String phonenumber, String password,Integer slat) {
Integer integer = addUserService.addUser(userid, username, phonenumber, password,slat);
if (integer == 1){
return "新建用户完成";
}else {
return "新建用户失败";
}
}
}5.测试

注册密码为111

数据库密码完成加密,如图所示
登录用户代码
首先需要个根据userid查找user的查找代码,这里我就不写了(为了获取slat)
1.登入用户mapper
@Repository
public interface LoginMapper {
@Select("select * from user where userid = #{userid} and password = #{password}")
UserVO login(Integer userid,String password);
}2.登入用户service
//接口层
public interface Md5LoginService {
UserVO login(Integer userid, String password);
}@Service
public class Md5LoginServiceImp implements Md5LoginService {
@Autowired
private LoginMapper loginMapper;
@Autowired
private FindUserbyIdMapper findUserbyIdMapper;
@Override
public UserVO login(Integer userid, String password) {
UserVO user = findUserbyIdMapper.findUserById(userid); //根据userid查找user
String salt = user.getSlat(); //获得当前用户的salt
String md5password = DigestUtils.md5Hex(password + salt); //获得加密密码
UserVO userVO = loginMapper.login(userid, md5password);
return userVO;
}
}主要逻辑是如果输入的未加密密码和slat正确,加密后得到的字段和数据库的password字段是相同的
3.登入用户controller
@RestController
public class Md5LoginController {
@Autowired
private Md5LoginService md5LoginService;
@RequestMapping("md5login")
public UserVO md5login (Integer userid,String password){
UserVO userVO = md5LoginService.login(userid, password);
return userVO;
}
}4.验证

登入成功(密码111)
登入失败,未作处理
边栏推荐
- Mockito unit testing
- STM8S-----选项字节
- [Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
- 跨境电商看不到另一面:商家刷单、平台封号、黑灰产牟利
- QNX Hypervisor 2.2 user manual] 10.1 gm vdev options
- Pine Script | How to display and typeset a plot switch?
- kingbaseES V8R2/R3 表在指定表空间,为何显示为默认表空间?
- 基地址:环境变量
- In the season of going overseas, the localization of Internet tips for going overseas
- [Original] Start the XPS/OXPS reader that comes with Windows 10
猜你喜欢

6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch

pytorch applied to MNIST handwritten font recognition

【项目实现】Boost搜索引擎

深度学习(三)分类 理论部分

new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail

In a more general sense, calculating the displacement distance and assumptions

There are too many systems, how to realize multi-account interworking?

Zabbix set up email alert + enterprise WeChat alert

系统太多,多账号互通如何实现?
sql注入一般流程(附例题)
随机推荐
【项目实现】Boost搜索引擎
返回字符串中的最大回文数
【医保科普】维护医保基金安全,我们可以这样做
Ant - the design of the Select component using a custom icon (suffixIcon attribute) suffixes, click on the custom ICONS have no reaction, will not display the drop-down menu
uni-app 从零开始-基础模版(一)
What is the source of flinkcdc consuming mysql binlog data without sqltype=delete
Exclude_reserved_words 排除关键字
"Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
WPE详细教程
In the season of going overseas, the localization of Internet tips for going overseas
The keytool command
一个属于程序员的七夕节!
MySQL Query Exercise (1)
C语言--环形缓存区
[Study Notes Dish Dog Learning C] Dynamic Memory Management
golang中的unsafe.Pointer,指针,引用
自定义通用分页标签02
如何读取 resources 目录下的文件路径?
【原创】启动Win10自带的XPS/OXPS阅读器