密码算法
This commit is contained in:
parent
7177f49918
commit
b6b635d7d8
@ -0,0 +1,86 @@
|
|||||||
|
package com.cdzy.common.utils;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.HexFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SHA-256 加盐加密工具类
|
||||||
|
*/
|
||||||
|
public class SHA256WithSaltUtil {
|
||||||
|
|
||||||
|
private static final int SALT_LENGTH = 16; // 盐值长度(16字节 = 128位)
|
||||||
|
private static final String ALGORITHM = "SHA-256";
|
||||||
|
private static final HexFormat HEX_FORMAT = HexFormat.of();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机盐值
|
||||||
|
*/
|
||||||
|
public static String generateSalt() {
|
||||||
|
SecureRandom random = new SecureRandom();
|
||||||
|
byte[] salt = new byte[SALT_LENGTH];
|
||||||
|
random.nextBytes(salt);
|
||||||
|
return Base64.getEncoder().encodeToString(salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密密码(SHA-256 + 盐值)
|
||||||
|
* @param password 原始密码
|
||||||
|
* @param salt 盐值(如果为null则自动生成)
|
||||||
|
* @return 返回加密后的密码和盐值组合字符串(格式:密文:盐值)
|
||||||
|
*/
|
||||||
|
public static String encrypt(String password, String salt) {
|
||||||
|
try {
|
||||||
|
// 如果未提供盐值,则生成新盐值
|
||||||
|
String usedSalt = (salt == null) ? generateSalt() : salt;
|
||||||
|
|
||||||
|
MessageDigest md = MessageDigest.getInstance(ALGORITHM);
|
||||||
|
md.update(Base64.getDecoder().decode(usedSalt)); // 加入盐值
|
||||||
|
byte[] hash = md.digest(password.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
// 返回格式:HEX(哈希值):Base64(盐值)
|
||||||
|
return HEX_FORMAT.formatHex(hash) + ":" + usedSalt;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("密码加密失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证密码
|
||||||
|
* @param inputPassword 用户输入的密码
|
||||||
|
* @param storedHash 数据库存储的加密字符串(格式:密文:盐值)
|
||||||
|
* @return 是否匹配
|
||||||
|
*/
|
||||||
|
public static boolean verify(String inputPassword, String storedHash) {
|
||||||
|
if (inputPassword == null || storedHash == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] parts = storedHash.split(":");
|
||||||
|
if (parts.length != 2) {
|
||||||
|
throw new IllegalArgumentException("存储的密码格式无效");
|
||||||
|
}
|
||||||
|
|
||||||
|
String inputHash = encrypt(inputPassword, parts[1]);
|
||||||
|
return inputHash.equals(storedHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试示例
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String password = "admin123";
|
||||||
|
|
||||||
|
// 加密
|
||||||
|
String encrypted = encrypt(password, null);
|
||||||
|
System.out.println("加密结果: " + encrypted);
|
||||||
|
|
||||||
|
// 验证
|
||||||
|
boolean matched = verify(password, encrypted);
|
||||||
|
System.out.println("验证结果: " + matched);
|
||||||
|
|
||||||
|
// 错误密码测试
|
||||||
|
boolean wrongMatch = verify("wrongPassword", encrypted);
|
||||||
|
System.out.println("错误密码测试: " + wrongMatch);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -54,7 +54,6 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler {
|
|||||||
if (ex instanceof NoResourceFoundException) {
|
if (ex instanceof NoResourceFoundException) {
|
||||||
msg = "路径不存在";
|
msg = "路径不存在";
|
||||||
}
|
}
|
||||||
ex.printStackTrace();
|
|
||||||
log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
|
log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
|
||||||
|
|
||||||
return webFluxResponseWriter(response, msg,null);
|
return webFluxResponseWriter(response, msg,null);
|
||||||
|
|||||||
@ -79,8 +79,8 @@ public class EbikeOperatorStaffController {
|
|||||||
*
|
*
|
||||||
* @return 运营商员工账户表详情
|
* @return 运营商员工账户表详情
|
||||||
*/
|
*/
|
||||||
@GetMapping("getInfo")
|
@GetMapping("getStaffInfo")
|
||||||
public JsonResult<?> getInfo() {
|
public JsonResult<?> getStaffInfo() {
|
||||||
StaffInfo info = ebikeOperatorStaffService.getStaffInfo();
|
StaffInfo info = ebikeOperatorStaffService.getStaffInfo();
|
||||||
return JsonResult.success(info);
|
return JsonResult.success(info);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,8 +15,13 @@ public class StaffInfo implements Serializable {
|
|||||||
|
|
||||||
private EbikeOperatorStaff staff;
|
private EbikeOperatorStaff staff;
|
||||||
|
|
||||||
private EbikeRole role;
|
// private List<EbikeRole> roles;
|
||||||
|
|
||||||
private List<EbikePermission> permissions;
|
private List<String> roles;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*权限码集合
|
||||||
|
*/
|
||||||
|
private List<String> permissions;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,9 @@ import com.mybatisflex.spring.service.impl.ServiceImpl;
|
|||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static com.cdzy.staff.model.entity.table.EbikeOperatorStaffTableDef.EBIKE_OPERATOR_STAFF;
|
import static com.cdzy.staff.model.entity.table.EbikeOperatorStaffTableDef.EBIKE_OPERATOR_STAFF;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,14 +49,20 @@ public class EbikeOperatorStaffServiceImpl extends ServiceImpl<EbikeOperatorStaf
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StaffInfo getStaffInfo() {
|
public StaffInfo getStaffInfo() {
|
||||||
Long loginId = (Long)StpUtil.getLoginId();
|
Long loginId = StpUtil.getLoginIdAsLong();
|
||||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
.where(EBIKE_OPERATOR_STAFF.STAFF_ID.eq(loginId));
|
.where(EBIKE_OPERATOR_STAFF.STAFF_ID.eq(loginId));
|
||||||
EbikeOperatorStaff ebikeOperatorStaff = staffMapper.selectOneByQuery(queryWrapper);
|
EbikeOperatorStaff ebikeOperatorStaff = staffMapper.selectOneByQuery(queryWrapper);
|
||||||
|
List<String> permissions = new ArrayList<>();
|
||||||
|
permissions.add("common:btn:add");
|
||||||
|
permissions.add("common:btn:edit");
|
||||||
|
permissions.add("common:btn:delete");
|
||||||
|
List<String> roles = new ArrayList<>();
|
||||||
|
permissions.add("common");
|
||||||
return StaffInfo.builder()
|
return StaffInfo.builder()
|
||||||
.staff(ebikeOperatorStaff)
|
.staff(ebikeOperatorStaff)
|
||||||
.role(null)
|
.roles(null)
|
||||||
.permissions(null)
|
.permissions(permissions)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
ebike-staff/src/test/java/com/cdzy/staff/PasswordTest.java
Normal file
19
ebike-staff/src/test/java/com/cdzy/staff/PasswordTest.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package com.cdzy.staff;
|
||||||
|
|
||||||
|
import com.cdzy.common.utils.SHA256WithSaltUtil;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = com.cdzy.staff.EbikeStaffApplication.class)
|
||||||
|
public class PasswordTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
String salt = SHA256WithSaltUtil.generateSalt();
|
||||||
|
System.out.println(salt);
|
||||||
|
String password = "123456";
|
||||||
|
String encrypt = SHA256WithSaltUtil.encrypt(password, salt);
|
||||||
|
System.out.println(encrypt);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user