Compare commits

...

6 Commits

14 changed files with 82 additions and 40 deletions

View File

@ -49,7 +49,7 @@
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-exception-handler</artifactId>
<version>0.0.6-SNAPSHOT</version>
<version>0.0.7-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -0,0 +1,18 @@
package xyz.zhouxy.plusone.util;
import java.util.concurrent.ThreadLocalRandom;
public final class RandomUtil {
private RandomUtil() {
throw new IllegalStateException("Utility class");
}
public static String randomStr(char[] sourceCharacters, int length) {
ThreadLocalRandom random = ThreadLocalRandom.current();
char[] result = new char[length];
for (int i = 0; i < length; i++) {
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];
}
return String.valueOf(result);
}
}

View File

@ -0,0 +1,23 @@
package xyz.zhouxy.plusone.util;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
class RandomUtilTests {
@Test
void testRandom() {
String[] s = new String[20];
for (int i = 0; i < 20; i++) {
s[i] = RandomUtil.randomStr(
"0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM~`!@#$%^&*()_-+={[\\|/:;\"',.<>?]}"
.toCharArray(),
28);
}
log.info("{}", Arrays.toString(s));
}
}

View File

@ -15,11 +15,6 @@
<groupId>xyz.zhouxy</groupId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.domain;
import cn.hutool.core.lang.UUID;
import java.util.UUID;
import lombok.Getter;
/**
@ -20,7 +21,7 @@ public abstract class DomainEvent {
private long happenedAt;
protected DomainEvent() {
this.identifier = UUID.randomUUID().toString(true);
this.identifier = UUID.randomUUID().toString();
this.happenedAt = System.currentTimeMillis();
}
}

View File

@ -86,18 +86,18 @@ public abstract class PlusoneJdbcDaoSupport {
return update(sql, new MapSqlParameterSource(paramName, value));
}
protected final int batchUpdate(String sql, SqlParameterSource[] batchArgs) {
protected final long batchUpdate(String sql, SqlParameterSource[] batchArgs) {
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
return NumberUtil.sum(i);
}
protected final <T> int batchUpdate(String sql, Stream<T> c,
protected final <T> long batchUpdate(String sql, Stream<T> c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
int[] i = this.jdbc.batchUpdate(sql, buildSqlParameterSourceArray(c, paramSourceBuilder));
return NumberUtil.sum(i);
}
protected final <T> int batchUpdate(String sql, Collection<T> c,
protected final <T> long batchUpdate(String sql, Collection<T> c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
int[] i = this.jdbc.batchUpdate(sql, buildSqlParameterSourceArray(c, paramSourceBuilder));
return NumberUtil.sum(i);

View File

@ -14,10 +14,6 @@
<groupId>xyz.zhouxy</groupId>
<artifactId>plusone-basic-common</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,19 +1,26 @@
package xyz.zhouxy.plusone.system.util;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.annotation.Nonnull;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.exception.BizException;
import xyz.zhouxy.plusone.util.RandomUtil;
/**
* 密码工具类
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@Slf4j
public final class PasswordUtil {
private static final String SALT_BASE_STRING = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/";
private static final char[] SALT_BASE_CHAR_ARRAY = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/"
.toCharArray();
/**
* 将密码和随机盐混合并进行哈希加密
@ -29,7 +36,7 @@ public final class PasswordUtil {
var passwordWithSalt = salt.substring(0, i)
+ password
+ salt.substring(1);
String sha512Hex = DigestUtil.sha512Hex(passwordWithSalt);
String sha512Hex = sha512Hex(passwordWithSalt);
if (sha512Hex == null) {
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!");
}
@ -42,11 +49,23 @@ public final class PasswordUtil {
* @return 生成的随机盐
*/
public static String generateRandomSalt() {
return RandomUtil.randomString(SALT_BASE_STRING, 24);
return RandomUtil.randomStr(SALT_BASE_CHAR_ARRAY, 24);
}
private PasswordUtil() {
// 不允许实例化
throw new IllegalStateException("Utility class");
}
private static String sha512Hex(String data) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
messageDigest.update(data.getBytes(StandardCharsets.UTF_8));
byte[] result = messageDigest.digest();
return new BigInteger(1, result).toString(16);
} catch (NoSuchAlgorithmException e) {
log.error("{}", e);
}
return null;
}
}

View File

@ -31,7 +31,7 @@ class AccountRoleRefDAO extends PlusoneJdbcDaoSupport {
}
void insertAccountRoleRefs(Long accountId, Set<Long> roleRefs) {
int i = batchUpdate(
long i = batchUpdate(
"INSERT INTO sys_account_role (account_id, role_id) VALUES (:accountId, :roleId)",
roleRefs,
(Long roleId) -> new MapSqlParameterSource()

View File

@ -21,11 +21,11 @@ class DictValueDAO extends PlusoneJdbcDaoSupport {
void updateDictValues(Dict entity) {
update("DELETE FROM sys_dict_value WHERE dict_type = :dictType",
"dictType", entity.getId().orElseThrow());
int i = insertDictValues(entity.getId().orElseThrow(), entity);
long i = insertDictValues(entity.getId().orElseThrow(), entity);
assertResultEquals(i, entity.count());
}
int insertDictValues(Long dictId, Dict entity) {
long insertDictValues(Long dictId, Dict entity) {
if (Objects.isNull(dictId) || Objects.isNull(entity) || CollectionUtils.isEmpty(entity.getValues())) {
return 0;
}

View File

@ -29,7 +29,7 @@ class RoleMenuRefDAO extends PlusoneJdbcDaoSupport {
}
void saveRoleMenuRefs(Long roleId, Role entity) {
int i = batchUpdate(
long i = batchUpdate(
"INSERT INTO sys_role_menu(role_id, menu_id) VALUES (:roleId, :menuId)",
entity.getMenus(),
menuRef -> new MapSqlParameterSource()

View File

@ -30,7 +30,7 @@ class RolePermissionRefDAO extends PlusoneJdbcDaoSupport {
}
void saveRolePermissionRefs(Long roleId, Role entity) {
int i = batchUpdate(
long i = batchUpdate(
"INSERT INTO sys_role_permission(role_id, permission_id) VALUES (:roleId, :permissionId)",
entity.getPermissions(),
actionRef -> new MapSqlParameterSource()

View File

@ -67,6 +67,10 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>

16
pom.xml
View File

@ -91,16 +91,7 @@
<artifactId>hutool-core</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@ -186,11 +177,6 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>