添加密码加密类以及登录逻辑修改

This commit is contained in:
Ethereal 2024-04-25 23:19:42 +08:00
parent 2c1da7b80b
commit 6b6664763f
2 changed files with 88 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import com.example.catchTheLetters.entity.User;
import com.example.catchTheLetters.model.vo.UserVo; import com.example.catchTheLetters.model.vo.UserVo;
import com.example.catchTheLetters.service.AuthService; import com.example.catchTheLetters.service.AuthService;
import com.example.catchTheLetters.service.RedisService; import com.example.catchTheLetters.service.RedisService;
import com.example.catchTheLetters.utils.DESUtil;
import com.example.catchTheLetters.utils.JwtUtil; import com.example.catchTheLetters.utils.JwtUtil;
import com.example.catchTheLetters.utils.R; import com.example.catchTheLetters.utils.R;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -30,6 +31,9 @@ public class AuthServiceImpl implements AuthService {
@Resource @Resource
private RedisService redisService; private RedisService redisService;
@Resource
private DESUtil desUtil;
@Override @Override
public R<String> login(LoginDto loginDto) { public R<String> login(LoginDto loginDto) {
// 根据用户名查询用户信息 // 根据用户名查询用户信息
@ -48,7 +52,8 @@ public class AuthServiceImpl implements AuthService {
if (Objects.isNull(user)) { if (Objects.isNull(user)) {
return R.fail("用户不存在"); return R.fail("用户不存在");
} }
if (!user.getPassword().equals(loginDto.getPassword())) { String verifyPwd = desUtil.SHA512(loginDto.getPassword());
if (!user.getPassword().equals(verifyPwd)) {
return R.fail("密码错误"); return R.fail("密码错误");
} }
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
@ -111,7 +116,7 @@ public class AuthServiceImpl implements AuthService {
} }
} }
user.setPassword(registerDto.getPassword()); user.setPassword(desUtil.SHA512(registerDto.getPassword()));
user.setCreateTime(System.currentTimeMillis()); user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis()); user.setUpdateTime(System.currentTimeMillis());
var insert = mongoTemplate.insert(user); var insert = mongoTemplate.insert(user);

View File

@ -0,0 +1,81 @@
package com.example.catchTheLetters.utils;
import org.springframework.stereotype.Component;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author 慕华
* @date 2024/4/25
* @Version 1.0
* @description
*/
@Component
public class DESUtil {
/**
* 传入文本内容返回 SHA-256
* @param strText 传入加密文本
* @return 加密后文本
*/
public String SHA256(final String strText) {
return SHA(strText, "SHA-256");
}
/**
* 传入文本内容返回 SHA-512
*
* @param strText 传入加密文本
* @return 加密后文本
*/
public String SHA512(final String strText) {
return SHA(strText, "SHA-512");
}
/**
* md5加密
* @param strText 传入加密文本
* @return 加密后文本
*/
public String SHAMD5(String strText) {
return SHA(strText, "MD5");
}
/**
* 字符串 SHA 加密
*/
private String SHA(final String strText, final String strType) {
// 返回值
String strResult = null;
// 是否是有效字符串
if (strText != null && strText.length() > 0) {
try {
// SHA 加密开始
// 创建加密对象 并传入加密类型
MessageDigest messageDigest = MessageDigest.getInstance(strType);
// 传入要加密的字符串
messageDigest.update(strText.getBytes());
// 得到 byte 类型结果
byte[] byteBuffer = messageDigest.digest();
// byte 转换为 string
StringBuilder strHexString = new StringBuilder();
// 遍历 byte buffer
for (byte b : byteBuffer) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
strHexString.append('0');
}
strHexString.append(hex);
}
// 得到返回结果
strResult = strHexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return strResult;
}
}