当前位置:网站首页>【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)
登入失败,未作处理
边栏推荐
- 【源码】使用深度学习训练一个游戏
- 6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
- 融云「音视频架构实践」技术专场【内含完整PPT】
- 一文看懂推荐系统:召回04:离散特征处理,one-hot编码和embedding特征嵌入
- 系统太多,多账号互通如何实现?
- [Playwright Test Tutorial] 5 minutes to get started
- esp8266-01s刷固件步骤
- 为什么用Selenium做自动化测试
- pnpm 是凭什么对 npm 和 yarn 降维打击的
- STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目
猜你喜欢

高效IO模型

Zabbix set up email alert + enterprise WeChat alert

sqoop ETL tool

docker+网桥+redis主从+哨兵模式

数组相关 内容 解析

网络工程师入门必懂华为认证体系,附系统学习路线分享

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

自定义通用分页标签01

Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).

Dong mingzhu live cold face away, when employees frequency low-level mistakes, no one can understand their products
随机推荐
如何读取 resources 目录下的文件路径?
SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
共n级台阶,每次可以上1级或2级台阶,有多少种上法?
Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
C language -- ring buffer
base address: environment variable
Architecture of the actual combat camp module three operations
异步编程解决方案 Generator生成器函数、iterator迭代器、async/await、Promise
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
Countdown to 2 days, the "New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" will kick off soon
pytorch applied to MNIST handwritten font recognition
Flink原理流程图简单记录
uni-app 从零开始-基础模版(一)
《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
十一种概率分布
在更一般意义上验算移位距离和假设
new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
unsafe.Pointer, pointer, reference in golang
数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书
Pine脚本 | 如何显示和排版绘图开关?