md5加密修改

This commit is contained in:
haozq 2024-11-15 18:20:14 +08:00
parent 3d57ce1260
commit 6f9631de24
11 changed files with 59 additions and 77 deletions

View File

@ -46,6 +46,11 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.8.0</version> <!-- 请根据需要选择最新的稳定版本 -->
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>

View File

@ -103,4 +103,7 @@ public class PartApplyAppController {
return service.uploadImage(request);
}
}

View File

@ -77,7 +77,7 @@ public class PartApplyAppServiceImp {
public ServerResponse getPaTypeList(PaTypeVo dto) {
try{
List<PaTypeVo> list=mapper.getPaTypeList(dto);
ServerResponse.createSuccess(list);
return ServerResponse.createSuccess(list);
}catch (Exception e){
log.error(e.toString(),e);
}

View File

@ -166,7 +166,7 @@ public class PartInputController {
* @return
*/
@GetMapping("getInputDetailList")
@DecryptAndVerify(decryptedClass = PartInputVo.class)
@DecryptAndVerify(decryptedClass = PartInputDetails.class)
public ServerResponse getInputDetailList(EncryptedReq<PartInputDetails> dto) {
List<PartInputDetails> list = service.getInputDetailList(dto.getData());;
return ServerResponse.createSuccess(list);

View File

@ -74,4 +74,13 @@ public class PartInputDetails {
@Excel(name = "备注", width = 10.0, orderNum = "6")
private String remark;
private String type;
private String name;
private String model;
}

View File

@ -45,5 +45,7 @@ public class SysUserEntity implements Serializable {
private String companyId;
private String salt;
}

View File

@ -14,6 +14,8 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Set;
@ -41,10 +43,17 @@ public class UserAuthenticationProvider implements AuthenticationProvider {
if (userInfo == null) {
throw new UsernameNotFoundException("用户名不存在");
}
if(!Md5Utils.validatePassword2(password, userInfo.getPassword())){
try {
//密码加密
String daya= Md5Utils.createPwdEncrypt(userName,Md5Utils.generateMD5(password).toUpperCase(),userInfo.getSalt());
if(!Md5Utils.validatePasswordBast64(daya, userInfo.getPassword())){
throw new BadCredentialsException("密码不正确");
}
} catch (NoSuchAlgorithmException e) {
throw new BadCredentialsException("密码不正确");
}
// 我们还要判断密码是否正确这里我们的密码使用BCryptPasswordEncoder进行加密的
// if (!new BCryptPasswordEncoder().matches(password, userInfo.getPassword())) {
// throw new BadCredentialsException("密码不正确");

View File

@ -74,6 +74,8 @@ public class SelfUserEntity implements Serializable, UserDetails {
*/
private String companyId ;
private String salt;
@Override
public Collection<GrantedAuthority> getAuthorities() {
return authorities;

View File

@ -1,5 +1,7 @@
package com.bonus.gzgqj.manager.security.jwt;
import org.apache.shiro.crypto.hash.Md5Hash;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -23,6 +25,10 @@ public class Md5Utils {
return bytesToHex(digest);
}
// 验证密码
public static boolean validatePassword(String input, String storedHash) {
try {
@ -33,6 +39,10 @@ public class Md5Utils {
}
}
public static boolean validatePasswordBast64(String password, String pwd) {
return password.equals(pwd);
}
public static boolean validatePassword2(String input, String storedHash) {
try {
@ -44,11 +54,20 @@ public class Md5Utils {
}
public static void main(String[] args) throws NoSuchAlgorithmException {
// 生成密码散列
String password = "1";
String hashedPassword = generateMD5(password);
System.out.println("Password is valid: " + hashedPassword);
// 验证密码
boolean isValid = validatePassword(password, hashedPassword);
System.out.println("Password is valid: " + isValid);
String pwd="Abc@123++";
String hashedPassword = generateMD5(pwd);
System.err.println(hashedPassword);
String daya= Md5Utils.createPwdEncrypt("bns",Md5Utils.generateMD5("Abc@123++").toUpperCase(),"946a175ccf87b28219f2ccbed6622dab");
System.err.println(daya);
boolean isValid =validatePasswordBast64(daya, "3NBzZK7qxhMhrmvi63FnuQ==");
System.err.println(isValid);
//MD5加盐算法
}
public static String createPwdEncrypt(String username,String password,String salt){
return new Md5Hash(password,username+salt,2).toBase64();
}
}

View File

@ -1,68 +0,0 @@
package com.bonus.gzgqj.manager.security.jwt;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class md5 {
// public static String getMD5(String str) {
// try {
// // 生成一个MD5加密计算摘要
// MessageDigest md = MessageDigest.getInstance("MD5");
// // 计算md5函数
// md.update(str.getBytes());
// // digest()最后确定返回md5 hash值返回值为8为字符串因为md5 hash值是16位的hex值实际上就是8位的字符
// // BigInteger函数则将8位的字符串转换成16位hex值用字符串来表示得到字符串形式的hash值
// return new BigInteger(1, md.digest()).toString(16);
// } catch (Exception e) {
// System.out.println("MD5加密出现错误");
// }
// return str;
// }
public static void main(String[] args) {
String pwd="Abc@123++";
//MD5加盐算法
System.out.println(getMD5WithSalt(pwd));
}
/**
* MD5加密工具类
*/
/**
* 获取MD5字符串
*/
public static String getMD5(String content) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(content.getBytes());
return getHashString(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
private static final String SALT = "946a175ccf87b28219f2ccbed6622dab";
/**
* 获取加盐的MD5字符串
*/
public static String getMD5WithSalt(String content) {
return getMD5(getMD5(content+SALT));
}
private static String getHashString(MessageDigest digest) {
StringBuilder builder = new StringBuilder();
for (byte b : digest.digest()) {
builder.append(Integer.toHexString((b >> 4) & 0xf));
builder.append(Integer.toHexString(b & 0xf));
}
return builder.toString();
}
}

View File

@ -6,6 +6,7 @@
<select id="selectUserByName" resultType="com.bonus.gzgqj.manager.core.entity.SysUserEntity">
SELECT t.ID AS userId,
t.NAME ,
t.SALT salt,
t.LOGIN_NAME username,
t.ORG_ID orgId,
t.COMPANY_ID companyId,