22 Commits

Author SHA1 Message Date
92896a0345 Merge branch 'dev' of http://zhouxy.xyz:3000/ZhouXY108/plusone-admin into dev 2023-05-16 17:39:21 +08:00
ac3fdae2d0 改用 ValueSet 的静态工厂方法。 2023-05-16 17:39:09 +08:00
8dcf8eadac 修改注释。 2023-05-16 17:38:37 +08:00
39d8eac578 删除多余的导入。 2023-04-28 23:42:15 +08:00
3858aa16e1 更新 Spring Boot 版本。 2023-04-28 23:41:15 +08:00
4d3d0f7cc7 commit. 2023-04-20 21:29:23 +08:00
17b5b55d59 添加些许规约。 2023-04-19 19:24:00 +08:00
30f29bb4b3 更改 Password 实现。 2023-04-19 06:05:02 +08:00
104bffb0e8 Merge branch 'dev' of http://zhouxy.xyz:3000/ZhouXY108/plusone-admin into dev 2023-04-16 04:33:31 +08:00
62607dc0a4 plusone-exception-handler 的代码调整。 2023-04-16 00:54:59 +08:00
fe03b6da4d 静态工厂方法添加 @StaticFactoryMethod 注解。 2023-04-16 00:16:11 +08:00
0f145e383e 重构 FastDFSFile。 2023-04-16 00:08:36 +08:00
89584d2a48 重构代码。 2023-04-16 00:08:35 +08:00
f3017e90c0 修改错别字。 2023-04-16 00:08:35 +08:00
f1d16808c6 添加常量。 2023-04-16 00:08:35 +08:00
65d77f35c1 静态工厂方法添加 @StaticFactoryMethod 注解。 2023-04-15 14:01:46 +08:00
b14c03fc32 静态工厂方法添加 @StaticFactoryMethod 注解。 2023-04-15 14:00:20 +08:00
ba34fa4a2b 重构 FastDFSFile。 2023-04-15 13:38:00 +08:00
b015f5d1c4 重构代码。 2023-04-15 13:37:44 +08:00
6335fa03b1 Merge branch 'dev' of http://zhouxy.xyz:3000/ZhouXY108/plusone-admin into dev 2023-04-15 12:26:02 +08:00
5215fded9c 修改错别字。 2023-04-06 16:21:54 +08:00
c30cca8f6a 添加常量。 2023-04-04 18:53:38 +08:00
47 changed files with 397 additions and 114 deletions

View File

@@ -18,4 +18,11 @@
目前项目还没完成,开发中……
相关的文档和介绍完善中……
相关的文档和介绍完善中……
## 编码规约
### 关于 null
1. 方法默认参数不为 `null`**可以**在 Javadoc 中对参数进行说明,如“...(must not be {@code null}[ or empty string])”。方法内部**必须**做参数校验,如使用 Assert 等工具类。
2. 如果参数允许为空,**必须**为参数添加 `@Nullable` 注解,并在 Javadoc 中对参数进行说明,如“...(may be {@code null})”。
3. 除极特殊的情况,方法默认**不返回 `null`**,如可能返回一个对象表示值的缺失,则**必须**使用 `Optional`**绝不在返回类型为 Optional 的方法中返回 `null`**。
4. 在极其特殊的情况下,方法需要返回 `null`**必须**给方法加上 `@Nullable` 注解,并在 Javadoc 中详细说明。

View File

@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.exception.handler.RestfulResult;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
/**
* 默认异常的处理器

View File

@@ -2,6 +2,8 @@ package xyz.zhouxy.plusone.constant;
public class ErrorCodeConsts {
public static final int DEFAULT_ERROR_CODE = 9999999;
public static final int DEFAULT_SYS_ERROR_CODE = 5000000;
public static final int DEFAULT_BIZ_ERROR_CODE = 4000000;
private ErrorCodeConsts() {
throw new IllegalStateException("Utility class");

View File

@@ -1,6 +1,6 @@
package xyz.zhouxy.plusone.util;
import java.util.concurrent.ThreadLocalRandom;
import java.security.SecureRandom;
public final class RandomUtil {
private RandomUtil() {
@@ -8,7 +8,7 @@ public final class RandomUtil {
}
public static String randomStr(char[] sourceCharacters, int length) {
ThreadLocalRandom random = ThreadLocalRandom.current();
SecureRandom random = new SecureRandom();
char[] result = new char[length];
for (int i = 0; i < length; i++) {
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];

View File

@@ -21,7 +21,7 @@ public final class EntityStatus extends Enumeration<EntityStatus> {
public static final EntityStatus AVAILABLE = new EntityStatus(0, "正常");
public static final EntityStatus DISABLED = new EntityStatus(1, "禁用");
private static final ValueSet<EntityStatus> VALUE_SET = new ValueSet<>(
private static final ValueSet<EntityStatus> VALUE_SET = ValueSet.of(
AVAILABLE, DISABLED);
public static EntityStatus of(int id) {

View File

@@ -7,8 +7,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import xyz.zhouxy.plusone.spring.SpringContextHolder;
/**
* 全局单例,由 Spring 装配好的对象。
* 可通过静态方法获取 Spring 容器中的 {@link JdbcTemplate} 和
* 通过静态方法获取 Spring 容器中的 {@link JdbcTemplate} 和
* {@link NamedParameterJdbcTemplate} 对象。
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>

View File

@@ -8,7 +8,7 @@ import java.sql.SQLException;
*
* <p>
* 通过在 {@link #map(ResultSet)} 中配置 {@link ResultSet} 到对象的映射,
* 可将 {@link #rowMapper(ResultSet, int)} 的方法用,
* 可将 {@link #rowMapper(ResultSet, int)} 的方法用,
* 直接当成 {@link org.springframework.jdbc.core.RowMapper} 对象传给
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* 的查询方法,

View File

@@ -1,8 +1,17 @@
package xyz.zhouxy.plusone.oss;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
@@ -14,7 +23,11 @@ import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.io.Files;
import lombok.Getter;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.exception.SysException;
public class FastDFSUtil {
@@ -44,7 +57,7 @@ public class FastDFSUtil {
* @throws FastDFSException
*/
public String[] upload(FastDFSFile file) throws FastDFSException {
logger.info("File Name: {}, File Length: {}", file.getName(), file.getContent().length);
logger.info("File Name: {}, File Length: {}", file.getFileName(), file.getContent().length);
NameValuePair[] metaList = new NameValuePair[1];
metaList[0] = new NameValuePair("author", file.getAuthor());
@@ -63,7 +76,7 @@ public class FastDFSUtil {
uploadResults[0], uploadResults[1], System.currentTimeMillis() - startTime);
} catch (IOException e) {
throw new FastDFSException("IO Exception when uploadind the file:" + file.getName(), e);
throw new FastDFSException("IO Exception when uploadind the file:" + file.getFileName(), e);
} catch (MyException e) {
throw new FastDFSException(e);
}
@@ -109,18 +122,72 @@ public class FastDFSUtil {
}
}
@Getter
public static final class FastDFSFile {
private String name;
private byte[] content;
private String ext;
private String md5;
private String author;
@Getter
private final String fileName;
private final byte[] content;
@Getter
private final String ext;
@Getter
private final String md5;
@Getter
private final String author;
public FastDFSFile(String name, byte[] content, String ext) {
this.name = name;
private FastDFSFile(@Nonnull File file, @Nullable String author) throws IOException {
this.fileName = file.getName();
this.content = Files.toByteArray(file);
this.ext = Files.getFileExtension(fileName);
this.md5 = md5Hex(content);
this.author = author;
}
private FastDFSFile(@Nonnull String fileName, @Nonnull byte[] content, @Nullable String author) {
this.fileName = fileName;
this.content = content;
this.ext = ext;
this.ext = Files.getFileExtension(fileName);
this.md5 = md5Hex(content);
this.author = author;
}
@Nonnull
@StaticFactoryMethod(FastDFSFile.class)
public static FastDFSFile of(@Nonnull File file) throws IOException {
return new FastDFSFile(file, null);
}
@Nonnull
@StaticFactoryMethod(FastDFSFile.class)
public static FastDFSFile of(@Nonnull File file, @Nullable String author) throws IOException {
return new FastDFSFile(file, author);
}
@Nonnull
@StaticFactoryMethod(FastDFSFile.class)
public static FastDFSFile of(@Nonnull String fileName, @Nonnull byte[] content) {
return new FastDFSFile(fileName, content, null);
}
@Nonnull
@StaticFactoryMethod(FastDFSFile.class)
public static FastDFSFile of(@Nonnull String fileName, @Nonnull byte[] content, @Nullable String author) {
return new FastDFSFile(fileName, content, author);
}
public byte[] getContent() {
return Arrays.copyOf(content, content.length);
}
@Nonnull
private static String md5Hex(byte[] data) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(data);
byte[] result = messageDigest.digest();
var sha512Hex = new BigInteger(1, result).toString(16);
return Objects.requireNonNull(sha512Hex);
} catch (NoSuchAlgorithmException e) {
throw new SysException(e);
}
}
}
}

View File

@@ -3,14 +3,10 @@ package xyz.zhouxy.plusone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.extern.slf4j.Slf4j;
@SpringBootApplication
@Slf4j
public class PlusoneApplication {
public static void main(String[] args) {
log.debug("Plusone started!");
SpringApplication.run(PlusoneApplication.class, args);
}

View File

@@ -1,12 +1,11 @@
package xyz.zhouxy.plusone;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.annotation.Resource;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@@ -24,10 +23,7 @@ class FastDFSTests {
@Test
void testOSS() throws FileNotFoundException, IOException, FastDFSException {
try (FileInputStream in = new FileInputStream("D:\\ZhouXY\\Desktop\\666.png");) {
byte[] content = IOUtils.toByteArray(in);
String[] upload = fastDFSUtil.upload(new FastDFSFile("666.png", content, "png"));
log.info(String.join("/", upload));
}
String[] upload = fastDFSUtil.upload(FastDFSFile.of(new File("D:\\ZhouXY\\Desktop\\666.png")));
log.info(String.join("/", upload));
}
}

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.exception;
package xyz.zhouxy.plusone.system.application.common.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.exception;
package xyz.zhouxy.plusone.system.application.common.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.exception;
package xyz.zhouxy.plusone.system.application.common.exception;
import xyz.zhouxy.plusone.validator.InvalidInputException;

View File

@@ -1,24 +1,22 @@
package xyz.zhouxy.plusone.system.application.exception.handler;
import javax.annotation.Nonnull;
package xyz.zhouxy.plusone.system.application.common.exception.handler;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.exception.handler.RestfulResult;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.system.application.common.exception.AccountLoginException;
@RestControllerAdvice
public class AccountLoginExceptionHandler extends BaseExceptionHandler {
public AccountLoginExceptionHandler(@Nonnull ExceptionInfoHolder exceptionInfoHolder) {
public AccountLoginExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
}
@ExceptionHandler({ AccountLoginException.class })
public ResponseEntity<RestfulResult> handleException(@Nonnull Exception e) {
public ResponseEntity<RestfulResult> handleException(Exception e) {
return buildExceptionResponse(e);
}
}

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.exception.handler;
package xyz.zhouxy.plusone.system.application.common.exception.handler;
import javax.annotation.Nonnull;
@@ -17,7 +17,7 @@ import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.exception.SameTokenInvalidException;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.exception.handler.RestfulResult;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
/**
* Sa-Token 异常处理器

View File

@@ -0,0 +1,138 @@
package xyz.zhouxy.plusone.system.application.common.model;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.OptionalDouble;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import xyz.zhouxy.plusone.exception.BizException;
public class AuthenticationInfo<T> {
/** token 值 */
private final String token;
/** 此 token 对应的LoginId未登录时不会构建此类的实例 */
private final T loginId;
/** 账号类型 */
private final String loginType;
/** 登录设备类型 */
private final String deviceType;
/** 自定义数据 */
@Nonnull
private final Map<String, Object> metadata;
private AuthenticationInfo(String token, T loginId, String loginType, String deviceType,
@Nonnull Map<String, Object> metadata) {
this.token = token;
this.loginId = loginId;
this.loginType = loginType;
this.deviceType = deviceType;
this.metadata = metadata;
}
public String getToken() {
return token;
}
public T getLoginId() {
return loginId;
}
public String getLoginType() {
return loginType;
}
public String getDeviceType() {
return deviceType;
}
private Object getObjFromMetadata(String key) {
if (this.metadata.containsKey(key)) {
return this.metadata.get(key);
}
throw new BizException(String.format("不存在 key 为 \"%s\"的元数据。", key));
}
public OptionalDouble getMetadataAsDouble(String key) {
Object valObj = getObjFromMetadata(key);
if (valObj instanceof Double val) {
return OptionalDouble.of(val.doubleValue());
}
return OptionalDouble.empty();
}
public OptionalLong getMetadataAsLong(String key) {
Object valObj = getObjFromMetadata(key);
if (valObj instanceof Long val) {
return OptionalLong.of(val.longValue());
}
return OptionalLong.empty();
}
public OptionalInt getMetadataAsInt(String key) {
Object valObj = getObjFromMetadata(key);
if (valObj instanceof Integer val) {
return OptionalInt.of(val.intValue());
}
return OptionalInt.empty();
}
public Optional<String> getMetadataAsString(String key) {
Object valObj = getObjFromMetadata(key);
if (valObj instanceof String val) {
return Optional.of(val);
}
return Optional.empty();
}
@SuppressWarnings("unchecked")
public <C> Optional<C> getMetadata(String key, Class<C> type) {
Object valObj = getObjFromMetadata(key);
if (valObj == null || !type.isAssignableFrom(valObj.getClass())) {
return Optional.empty();
}
return Optional.of((C) valObj);
}
// ==========================================
// builder
public static <T> Builder<T> builder(String token, T loginId, String loginType, String deviceType) {
return new Builder<>(token, loginId, loginType, deviceType);
}
public static final class Builder<T> {
private final String token;
private final T loginId;
private final String loginType;
private final String deviceType;
@Nonnull
private final Map<String, Object> meta = new ConcurrentHashMap<>();
private Builder(String token, T loginId, String loginType, String deviceType) {
this.token = token;
this.loginId = loginId;
this.loginType = loginType;
this.deviceType = deviceType;
}
public <C> Builder<T> putMetadata(String key, @Nullable C value) {
this.meta.put(key, value);
return this;
}
public AuthenticationInfo<T> build() {
return new AuthenticationInfo<>(token, loginId, loginType, deviceType, meta);
}
}
}

View File

@@ -0,0 +1,21 @@
package xyz.zhouxy.plusone.system.application.common.model;
public class PlusoneContext {
private static final ThreadLocal<AuthenticationInfo<Long>> context = new ThreadLocal<>();
public static void setContext(AuthenticationInfo<Long> value) {
context.set(value);
}
public static AuthenticationInfo<Long> getContext() {
return context.get();
}
public static void remove() {
context.remove();
}
private PlusoneContext() {
throw new IllegalStateException("Utility class");
}
}

View File

@@ -8,9 +8,9 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.dev33.satoken.stp.StpLogic;
import xyz.zhouxy.plusone.system.application.common.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedPrincipalTypeException;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries;
import xyz.zhouxy.plusone.system.application.query.result.AccountDetails;
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;

View File

@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import xyz.zhouxy.plusone.commons.util.PageDTO;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.application.common.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries;
import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams;
import xyz.zhouxy.plusone.system.application.query.result.AccountDetails;
@@ -94,7 +94,6 @@ public class AccountManagementService {
Account account = accountRepository.find(id)
.orElseThrow(() -> new DataNotExistException("该账号不存在"));
account.setAccountInfo(command.getNickname(), command.getAvatar(), Sex.of(command.getSex()));
account.setUpdatedBy(adminAuthLogic.getLoginIdAsLong());
accountRepository.save(account);
}

View File

@@ -6,10 +6,10 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Assert;
import xyz.zhouxy.plusone.system.application.common.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedPrincipalTypeException;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalType;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries;
import xyz.zhouxy.plusone.system.application.query.result.LoginInfoViewObject;
import xyz.zhouxy.plusone.system.application.service.command.LoginByOtpCommand;

View File

@@ -9,8 +9,8 @@ import org.springframework.util.Assert;
import cn.hutool.core.util.RandomUtil;
import xyz.zhouxy.plusone.mail.MailService;
import xyz.zhouxy.plusone.sms.SmsService;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.application.common.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.common.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository;
import xyz.zhouxy.plusone.system.domain.model.account.Email;
import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone;

View File

@@ -16,7 +16,7 @@ import org.springframework.util.Assert;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.domain.IWithOrderNumber;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.exception.UnsupportedMenuTypeException;
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedMenuTypeException;
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
import xyz.zhouxy.plusone.system.application.service.command.CreateMenuCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateMenuCommand;

View File

@@ -5,10 +5,10 @@ import java.util.Set;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouxy.plusone.system.application.common.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedPrincipalTypeException;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalType;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.application.service.command.RegisterAccountCommand;
import xyz.zhouxy.plusone.system.domain.model.account.Account;
import xyz.zhouxy.plusone.system.domain.model.account.AccountInfo;

View File

@@ -0,0 +1,20 @@
package xyz.zhouxy.plusone.system.application.web.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
HandlerInterceptor[] interceptors;
@Override
public void addInterceptors(InterceptorRegistry registry) {
for (var interceptor : interceptors) {
registry.addInterceptor(interceptor);
}
}
}

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.controller;
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.controller;
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.controller;
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.controller;
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.controller;
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.controller;
package xyz.zhouxy.plusone.system.application.web.controller;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;

View File

@@ -1,4 +1,4 @@
package xyz.zhouxy.plusone.system.application.controller;
package xyz.zhouxy.plusone.system.application.web.controller;
import javax.validation.Valid;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;

View File

@@ -0,0 +1,32 @@
package xyz.zhouxy.plusone.system.application.web.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.system.application.common.model.PlusoneContext;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;;
@Slf4j
@Order(1)
@Component
public class HttpContextInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
log.info("" + adminAuthLogic.isLogin());
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
PlusoneContext.remove();
log.info("拦截器清理 ThreadLocal防止内存泄漏");
}
}

View File

@@ -0,0 +1,7 @@
/**
* 拦截器
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @since 1.0
*/
package xyz.zhouxy.plusone.system.application.web.interceptor;

View File

@@ -1,15 +1,12 @@
package xyz.zhouxy.plusone.system.util;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.annotation.Nonnull;
import org.springframework.util.Assert;
import com.google.common.hash.Hashing;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.exception.BizException;
import xyz.zhouxy.plusone.util.RandomUtil;
/**
@@ -17,7 +14,6 @@ import xyz.zhouxy.plusone.util.RandomUtil;
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@Slf4j
public final class PasswordUtil {
private static final char[] SALT_BASE_CHAR_ARRAY = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/"
.toCharArray();
@@ -29,18 +25,18 @@ public final class PasswordUtil {
* @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 = sha512Hex(passwordWithSalt);
if (sha512Hex == null) {
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!");
}
return sha512Hex;
public static String hashPassword(String password, String salt) {
Assert.notNull(password, "Password must not be null");
Assert.notNull(salt, "Salt must not be null");
return Hashing.sha512().newHasher()
.putInt(Arrays.hashCode(salt.toCharArray()))
.putString(password, StandardCharsets.UTF_8)
.putInt(password.length())
.putBoolean(password.length() % 2 == 0)
.putString(salt, StandardCharsets.UTF_8)
.putInt(Arrays.hashCode(password.toCharArray()))
.hash()
.toString();
}
/**
@@ -56,16 +52,4 @@ public final class 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

@@ -6,6 +6,7 @@ import java.util.Optional;
import java.util.Set;
import lombok.ToString;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IWithVersion;
import xyz.zhouxy.plusone.exception.UserOperationException;
@@ -164,6 +165,7 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
return newInstance;
}
@StaticFactoryMethod(Account.class)
public static Account register(
Username username,
Email email,
@@ -193,6 +195,7 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
password, status, accountInfo, roleRefs, createdBy, updatedBy, version);
}
@StaticFactoryMethod(Account.class)
public static Account newInstance(
String username,
String email,
@@ -210,6 +213,7 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
return newInstance;
}
@StaticFactoryMethod(Account.class)
public static Account register(
String username,
String email,
@@ -265,10 +269,6 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
return Optional.ofNullable(updatedBy);
}
public void setUpdatedBy(long updatedBy) {
this.updatedBy = updatedBy;
}
@Override
public long getVersion() {
return this.version;

View File

@@ -4,6 +4,7 @@ import java.util.Objects;
import java.util.regex.Pattern;
import cn.hutool.core.util.DesensitizedUtil;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/**
@@ -26,10 +27,12 @@ public class Email extends Principal {
}
}
@StaticFactoryMethod(Email.class)
public static Email of(String email) {
return new Email(email);
}
@StaticFactoryMethod(Email.class)
public static Email ofNullable(String email) {
return Objects.nonNull(email) ? new Email(email) : null;
}

View File

@@ -4,6 +4,7 @@ import java.util.Objects;
import java.util.regex.Pattern;
import cn.hutool.core.util.DesensitizedUtil;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/**
@@ -26,10 +27,12 @@ public class MobilePhone extends Principal {
}
}
@StaticFactoryMethod(MobilePhone.class)
public static MobilePhone of(String mobilePhone) {
return new MobilePhone(mobilePhone);
}
@StaticFactoryMethod(MobilePhone.class)
public static MobilePhone ofNullable(String mobilePhone) {
return Objects.nonNull(mobilePhone) ? new MobilePhone(mobilePhone) : null;
}

View File

@@ -3,6 +3,7 @@ package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Objects;
import java.util.regex.Pattern;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.domain.ValidatableStringRecord;
@@ -26,10 +27,12 @@ public class Nickname extends ValidatableStringRecord {
}
}
@StaticFactoryMethod(Nickname.class)
public static Nickname of(String nickname) {
return new Nickname(nickname);
}
@StaticFactoryMethod(Nickname.class)
public static Nickname ofNullable(String nickname) {
return Objects.nonNull(nickname) ? new Nickname(nickname) : null;
}

View File

@@ -3,14 +3,11 @@ package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Objects;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import org.springframework.util.Assert;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.domain.IValueObject;
import xyz.zhouxy.plusone.exception.BizException;
import xyz.zhouxy.plusone.system.util.PasswordUtil;
/**
@@ -23,43 +20,35 @@ public class Password implements IValueObject {
private static final Pattern PATTERN = PatternConsts.PASSWORD;
private static final String DEFAULT_PASSWORD = "A1b2C3d4";
@Nonnull
private final String passwordVal;
@Nonnull
private final String saltVal;
private Password(String password) {
if (password == null) {
throw new IllegalArgumentException("密码不能为空");
}
if (!PATTERN.matcher(password).matches()) {
throw new IllegalArgumentException("密码格式不符合要求");
}
var salt = PasswordUtil.generateRandomSalt();
if (salt == null) {
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:生成随机盐失败");
}
Assert.notNull(password, "密码不能为空");
Assert.isTrue(PATTERN.matcher(password).matches(), "密码格式不符合要求");
String salt = PasswordUtil.generateRandomSalt();
this.saltVal = salt;
this.passwordVal = PasswordUtil.hashPassword(password, salt);
}
private Password(String password, String salt) {
if (password == null || salt == null) {
throw new IllegalArgumentException("password 和 salt 不能为空");
}
Assert.isTrue(password != null && salt != null, "password 和 salt 不能为空");
this.passwordVal = password;
this.saltVal = salt;
}
@StaticFactoryMethod(Password.class)
public static Password of(String password, String salt) {
return new Password(password, salt);
}
@StaticFactoryMethod(Password.class)
public static Password newPassword(String newPassword, String passwordConfirmation) {
Assert.isTrue(Objects.equals(newPassword, passwordConfirmation), "两次输入的密码不一致");
return newPassword(newPassword);
}
@StaticFactoryMethod(Password.class)
public static Password newPassword(String newPassword) {
return new Password(newPassword);
}
@@ -80,6 +69,7 @@ public class Password implements IValueObject {
return saltVal;
}
@StaticFactoryMethod(Nickname.class)
public static Password newDefaultPassword() {
return newPassword(DEFAULT_PASSWORD);
}

View File

@@ -4,6 +4,7 @@ import java.util.Collection;
import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.util.Enumeration;
import xyz.zhouxy.plusone.domain.IValueObject;
@@ -26,6 +27,7 @@ public final class Sex extends Enumeration<Sex> implements IValueObject {
private static final ValueSet<Sex> VALUE_SET = new ValueSet<>(UNSET, MALE, FEMALE);
@StaticFactoryMethod(Sex.class)
public static Sex of(int value) {
return VALUE_SET.get(value);
}

View File

@@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.regex.Pattern;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/**
@@ -24,6 +25,7 @@ public class Username extends Principal {
}
}
@StaticFactoryMethod(Username.class)
public static Username of(String username) {
return new Username(username);
}

View File

@@ -8,6 +8,7 @@ import java.util.Optional;
import java.util.Set;
import lombok.ToString;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IWithLabel;
import xyz.zhouxy.plusone.domain.IWithVersion;
@@ -70,12 +71,14 @@ public class Dict extends AggregateRoot<Long> implements IWithLabel, IWithVersio
this.version = version;
}
@StaticFactoryMethod(Dict.class)
public static Dict newInstance(
String dictType,
String dictLabel) {
return new Dict(null, dictType, dictLabel, Collections.emptySet(), 0);
}
@StaticFactoryMethod(Dict.class)
public static Dict newInstance(
String dictType,
String dictLabel,
@@ -83,6 +86,7 @@ public class Dict extends AggregateRoot<Long> implements IWithLabel, IWithVersio
return new Dict(null, dictType, dictLabel, values, 0);
}
@StaticFactoryMethod(Dict.class)
public static Dict newInstance(
String dictType,
String dictLabel,

View File

@@ -4,6 +4,7 @@ import java.util.Objects;
import lombok.Getter;
import lombok.ToString;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.IValueObject;
/**
@@ -25,6 +26,7 @@ public class DictValue implements IValueObject {
this.label = label;
}
@StaticFactoryMethod(DictValue.class)
public static DictValue of(int key, String label) {
return new DictValue(key, label);
}

View File

@@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.system.domain.model.menu;
import java.util.List;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType;
@@ -16,6 +17,7 @@ public class MenuConstructor {
throw new IllegalStateException("Utility class");
}
@StaticFactoryMethod(Menu.class)
public static Menu newMenuItem(
long parentId,
String path,
@@ -39,6 +41,7 @@ public class MenuConstructor {
remarks, component, cache, resource, actions, 0L);
}
@StaticFactoryMethod(Menu.class)
public static Menu newMenuList(
long parentId,
String path,

View File

@@ -3,6 +3,7 @@ package xyz.zhouxy.plusone.system.domain.model.permission;
import java.util.Optional;
import lombok.Getter;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.Entity;
import xyz.zhouxy.plusone.domain.IWithLabel;
import xyz.zhouxy.plusone.domain.IWithVersion;
@@ -20,7 +21,7 @@ public class Action extends Entity<Long> implements IWithLabel, IWithVersion {
@Getter String label;
@Getter long version;
public Action(Long id, String resource, String identifier, String label, long version) {
private Action(Long id, String resource, String identifier, String label, long version) {
this.id = id;
this.resource = resource;
this.identifier = identifier;
@@ -28,10 +29,12 @@ public class Action extends Entity<Long> implements IWithLabel, IWithVersion {
this.version = version;
}
@StaticFactoryMethod(Action.class)
static Action newInstance(String resource, String identifier, String label) {
return new Action(null, resource, identifier, label, 0L);
}
@StaticFactoryMethod(Action.class)
static Action existingInstance(Long id, String resource, String action, String label, Long version) {
return new Action(id, resource, action, label, version);
}

View File

@@ -6,6 +6,7 @@ import java.util.Objects;
import java.util.Optional;
import lombok.Getter;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IWithVersion;
@@ -37,6 +38,7 @@ public class Permission extends AggregateRoot<Long> implements IWithVersion {
// ==================== 实例化 ====================
@StaticFactoryMethod(Permission.class)
public static Permission newInstance(String resource) {
return new Permission(
null, resource,

View File

@@ -25,7 +25,7 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-boot.version>2.7.10</spring-boot.version>
<spring-boot.version>2.7.11</spring-boot.version>
<sa-token.version>1.34.0</sa-token.version>
<hutool.version>5.8.16</hutool.version>
<mybatis-starter.version>3.0.1</mybatis-starter.version>