update:功能
This commit is contained in:
@@ -8,8 +8,6 @@
|
||||
<artifactId>ski-dashboard</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.ski.lichuan</groupId>
|
||||
<artifactId>ski-dashboard-common</artifactId>
|
||||
|
||||
<properties>
|
||||
@@ -38,4 +36,16 @@
|
||||
<version>3.0.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- 排除Spring Boot Maven插件,因为这是一个数据模型模块,不是可执行应用程序 -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.ski.lichuan.common;
|
||||
|
||||
//TIP 要<b>运行</b>代码,请按 <shortcut actionId="Run"/> 或
|
||||
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
|
||||
// 查看 IntelliJ IDEA 建议如何修正。
|
||||
System.out.printf("Hello and welcome!");
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
//TIP 按 <shortcut actionId="Debug"/> 开始调试代码。我们已经设置了一个 <icon src="AllIcons.Debugger.Db_set_breakpoint"/> 断点
|
||||
// 但您始终可以通过按 <shortcut actionId="ToggleLineBreakpoint"/> 添加更多断点。
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.ski.lichuan.common.exception;
|
||||
|
||||
public class BusinessException extends RuntimeException {
|
||||
private Integer code;
|
||||
|
||||
public BusinessException(String message) {
|
||||
super(message);
|
||||
this.code = 500;
|
||||
}
|
||||
|
||||
public BusinessException(Integer code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public BusinessException(Integer code, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.ski.lichuan.common.exception;
|
||||
|
||||
import com.ski.lichuan.model.common.HttpResponseData;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 全局异常处理器
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
/**
|
||||
* 处理自定义业务异常
|
||||
*/
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public HttpResponseData handleBusinessException(BusinessException e) {
|
||||
log.error("业务异常: ", e);
|
||||
return new HttpResponseData<>().error(e.getCode(), e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理参数验证异常(MethodArgumentNotValidException)
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public HttpResponseData handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||
log.error("参数校验异常: ", e);
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
StringBuilder errorMsg = new StringBuilder();
|
||||
|
||||
if (bindingResult.hasErrors()) {
|
||||
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
|
||||
fieldErrors.forEach(error ->
|
||||
errorMsg.append(error.getField()).append(": ").append(error.getDefaultMessage()).append("; ")
|
||||
);
|
||||
}
|
||||
|
||||
return new HttpResponseData<>().error(400, "参数校验失败: " + errorMsg.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理参数绑定异常(BindException)
|
||||
*/
|
||||
@ExceptionHandler(BindException.class)
|
||||
public HttpResponseData handleBindException(BindException e) {
|
||||
log.error("参数绑定异常: ", e);
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
StringBuilder errorMsg = new StringBuilder();
|
||||
|
||||
if (bindingResult.hasErrors()) {
|
||||
List<FieldError> fieldErrors = bindingResult.getFieldErrors();
|
||||
fieldErrors.forEach(error ->
|
||||
errorMsg.append(error.getField()).append(": ").append(error.getDefaultMessage()).append("; ")
|
||||
);
|
||||
}
|
||||
|
||||
return new HttpResponseData<>().error(400, "参数绑定失败: " + errorMsg.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理IllegalArgumentException异常
|
||||
*/
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public HttpResponseData handleIllegalArgumentException(IllegalArgumentException e) {
|
||||
log.error("非法参数异常: ", e);
|
||||
return new HttpResponseData<>().error(400, e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理其他所有异常
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
public HttpResponseData handleException(Exception e) {
|
||||
log.error("系统异常: ", e);
|
||||
return new HttpResponseData<>().error(500, "系统内部错误,请联系管理员");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.ski.lichuan.common.utils;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* MD5加密工具类
|
||||
*/
|
||||
public class MD5Util {
|
||||
|
||||
/**
|
||||
* 对字符串进行MD5加密
|
||||
* @param input 待加密的字符串
|
||||
* @return 加密后的十六进制字符串
|
||||
*/
|
||||
public static String encrypt(String input) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
BigInteger no = new BigInteger(1, messageDigest);
|
||||
StringBuilder hashText = new StringBuilder(no.toString(16));
|
||||
|
||||
// 补齐前面的0,确保是32位
|
||||
while (hashText.length() < 32) {
|
||||
hashText.insert(0, "0");
|
||||
}
|
||||
|
||||
return hashText.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("MD5 algorithm not found", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证明文与MD5密文是否匹配
|
||||
* @param rawText 明文
|
||||
* @param md5Text MD5密文
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public static boolean verify(String rawText, String md5Text) {
|
||||
if (rawText == null || md5Text == null) {
|
||||
return false;
|
||||
}
|
||||
return md5Text.equals(encrypt(rawText));
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.ski.lichuan.mapper;
|
||||
|
||||
import com.ski.lichuan.model.auth.SysUser;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
@Select("SELECT id, username, password, nickname, status FROM sys_user WHERE username = #{username}")
|
||||
SysUser selectByUsername(@Param("username") String username);
|
||||
|
||||
@Insert("INSERT INTO sys_user(username, password, nickname, status) VALUES(#{username}, #{password}, #{nickname}, #{status})")
|
||||
@Options(useGeneratedKeys = true, keyProperty = "id")
|
||||
int insertUser(SysUser user);
|
||||
|
||||
@Select("SELECT id, username, password, nickname, status FROM sys_user WHERE id = #{id}")
|
||||
SysUser selectById(@Param("id") Long id);
|
||||
}
|
||||
Reference in New Issue
Block a user