添加异常处理
This commit is contained in:
parent
f70867e2c1
commit
e8e5deda8d
@ -30,7 +30,8 @@ CREATE TABLE IF NOT EXISTS user_role (
|
||||
add_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
|
||||
update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
INDEX idx_user_id(user_id),
|
||||
INDEX idx_role_id(role_id)
|
||||
INDEX idx_role_id(role_id),
|
||||
UNIQUE KEY uk_user_role (user_id, role_id)
|
||||
) COMMENT='用户与角色关联表';
|
||||
|
||||
|
||||
@ -52,7 +53,8 @@ CREATE TABLE IF NOT EXISTS role_file_rule (
|
||||
remark VARCHAR(255) COMMENT '规则说明',
|
||||
add_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
|
||||
update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
INDEX idx_role_id(role_id)
|
||||
INDEX idx_role_id(role_id),
|
||||
UNIQUE KEY uk_role_pattern (role_id, file_pattern)
|
||||
) COMMENT='角色-文件可访问规则表';
|
||||
|
||||
-- 6. 用户token表
|
||||
@ -83,3 +85,4 @@ CREATE TABLE IF NOT EXISTS user_file (
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
package com.knowledge.base.application.aspect;
|
||||
|
||||
import com.knowledge.base.application.exceptions.AppException;
|
||||
import com.knowledge.base.domain.common.exception.DomainException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(DomainException.class)
|
||||
public ResponseEntity<?> handleDomainException(DomainException e) {
|
||||
return ResponseEntity.badRequest().body(Map.of("code", 400, "msg", e.getMessage()));
|
||||
}
|
||||
|
||||
@ExceptionHandler(AppException.class)
|
||||
public ResponseEntity<?> handleAppException(AppException e) {
|
||||
return ResponseEntity.status(500).body(Map.of("code", 500, "msg", "系统异常:" + e.getMessage()));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<?> handleOtherException(Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return ResponseEntity.status(500).body(Map.of("code", 500, "msg", "服务器繁忙,请稍后重试"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
package com.knowledge.base.application.exceptions;
|
||||
|
||||
public class AppException extends RuntimeException {
|
||||
public AppException(String msg) { super(msg); }
|
||||
public AppException(String msg, Throwable cause) { super(msg, cause); }
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
package com.knowledge.base.application.exceptions;
|
||||
|
||||
/**
|
||||
* @author Luke.ye
|
||||
* @date 2025/5/6 08:47
|
||||
*/
|
||||
public class BizException extends RuntimeException {
|
||||
|
||||
private int code;
|
||||
|
||||
public BizException(int code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public BizException(int code, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@ -33,9 +33,7 @@ public class AuthFilter implements Filter {
|
||||
String path = request.getRequestURI();
|
||||
|
||||
// 需要鉴权的路径前缀
|
||||
if (path.startsWith("/search")
|
||||
|| path.startsWith("/api/v1/doc")
|
||||
) {
|
||||
if (path.startsWith("/api/v1/doc")) {
|
||||
String token = request.getHeader("Authorization");
|
||||
|
||||
if (StrUtil.isBlank(token)) {
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
package com.knowledge.base.domain.common.exception;
|
||||
|
||||
public class DomainException extends RuntimeException {
|
||||
public DomainException(String msg) { super(msg); }
|
||||
public DomainException(String msg, Throwable cause) { super(msg, cause); }
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
package com.knowledge.base.domain.user.service.iface;
|
||||
|
||||
import com.knowledge.base.domain.user.model.*;
|
||||
import com.knowledge.base.domain.user.repository.po.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@ -6,22 +6,46 @@ import com.knowledge.base.infrastructure.config.DynamicConfig;
|
||||
import com.knowledge.base.infrastructure.north.dto.user.UserDTO;
|
||||
import com.knowledge.base.infrastructure.north.dto.user.UserTokenDTO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/user")
|
||||
@RequiredArgsConstructor
|
||||
public class UserWriteController {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(UserWriteController.class);
|
||||
|
||||
private final UserAppService userAppService;
|
||||
private final DynamicConfig dynamicConfig;
|
||||
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<?> register(@RequestBody Map<String, Object> body) {
|
||||
String username = (String) body.getOrDefault("username", "");
|
||||
String password = (String) body.getOrDefault("password", "");
|
||||
List<Integer> roleIdInts = (List<Integer>) body.getOrDefault("roleIds", List.of(3));
|
||||
List<Long> roleIds = roleIdInts.stream().map(Integer::longValue).collect(Collectors.toList());
|
||||
|
||||
if (username.isBlank() || password.isBlank()) {
|
||||
return ResponseEntity.badRequest().body(Map.of("msg", "用户名或密码不能为空"));
|
||||
}
|
||||
|
||||
boolean ok = userAppService.registerUser(username, password, roleIds);
|
||||
if (!ok) {
|
||||
return ResponseEntity.badRequest().body(Map.of("msg", "注册失败,用户名已存在"));
|
||||
}
|
||||
return ResponseEntity.ok(Map.of("msg", "注册成功"));
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<?> login(@RequestBody Map<String, String> body, HttpServletResponse response) {
|
||||
String username = body.getOrDefault("username", "").trim();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user