first commit.
This commit is contained in:
23
plusone-system/plusone-system-common/pom.xml
Normal file
23
plusone-system/plusone-system-common/pom.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>plusone-system</artifactId>
|
||||
<groupId>xyz.zhouxy</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>plusone-system-common</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy</groupId>
|
||||
<artifactId>plusone-basic-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-crypto</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,20 @@
|
||||
package xyz.zhouxy.plusone.system.constant;
|
||||
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
|
||||
/**
|
||||
* 维护 StpLogic 的实例
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public class AuthLogic {
|
||||
private AuthLogic() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static final String ADMIN_LOGIN_TYPE = "Admin";
|
||||
public static final StpLogic adminAuthLogic = new StpLogic(ADMIN_LOGIN_TYPE);
|
||||
|
||||
public static final String USER_LOGIN_TYPE = "User";
|
||||
public static final StpLogic userAuthLogic = new StpLogic(USER_LOGIN_TYPE);
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package xyz.zhouxy.plusone.system.util;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import xyz.zhouxy.plusone.exception.PlusoneException;
|
||||
|
||||
/**
|
||||
* 密码工具类
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public final class PasswordUtil {
|
||||
private static final String SALT_BASE_STRING = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/";
|
||||
|
||||
/**
|
||||
* 将密码和随机盐混合,并进行哈希加密。
|
||||
*
|
||||
* @param password 密文密码
|
||||
* @param salt 随机盐
|
||||
* @return 哈希加密的结果
|
||||
*/
|
||||
@Nonnull
|
||||
public static String hashPassword(@Nonnull String password, @Nonnull String salt) {
|
||||
int length = salt.length();
|
||||
int i = length > 0 ? length / 2 : 0;
|
||||
var passwordWithSalt = salt.substring(0, i)
|
||||
+ password
|
||||
+ salt.substring(1);
|
||||
String sha512Hex = DigestUtil.sha512Hex(passwordWithSalt);
|
||||
if (sha512Hex == null) {
|
||||
throw new PlusoneException(9999999, "未知错误:哈希加密失败!");
|
||||
}
|
||||
return sha512Hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 24 位的字符串
|
||||
*
|
||||
* @return 生成的随机盐
|
||||
*/
|
||||
public static String generateRandomSalt() {
|
||||
return RandomUtil.randomString(SALT_BASE_STRING, 24);
|
||||
}
|
||||
|
||||
private PasswordUtil() {
|
||||
// 不允许实例化
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user