浏览代码

修改datasource默认配置项;增加通用ForbiddenException与API层的异常解析处理

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 3 年之前
父节点
当前提交
41b86c017a

+ 48 - 0
core-models/src/main/java/com/qmth/boot/core/exception/ForbiddenException.java

@@ -0,0 +1,48 @@
+package com.qmth.boot.core.exception;
+
+/**
+ * 禁止访问的运行时异常,在API层会按403状态码处理
+ */
+public class ForbiddenException extends RuntimeException implements CodeNameException {
+
+    private static final long serialVersionUID = 1014093321734424324L;
+
+    private Integer code;
+
+    private String name;
+
+    public ForbiddenException(String message) {
+        super(message);
+    }
+
+    public ForbiddenException(String message, Throwable t) {
+        super(message, t);
+    }
+
+    /**
+     * 带所有参数的构造方法
+     *
+     * @param code    - 数字标识,范围在100~999
+     * @param name    - 异常详细类型
+     * @param message - 文字提示
+     * @param cause   - 触发来源
+     */
+    public ForbiddenException(Integer code, String name, String message, Throwable cause) {
+        super(message, cause);
+        if (code != null) {
+            if (code < 100 || code > 999) {
+                throw new IllegalArgumentException("ForbiddenException.code should between 100~999");
+            }
+        }
+        this.code = code;
+        this.name = name;
+    }
+
+    public Integer getCode() {
+        return code;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

+ 31 - 1
core-models/src/main/java/com/qmth/boot/core/exception/UnauthorizedException.java

@@ -3,10 +3,14 @@ package com.qmth.boot.core.exception;
 /**
  * 未授权的运行时异常,在API层会按401状态码处理
  */
-public class UnauthorizedException extends RuntimeException {
+public class UnauthorizedException extends RuntimeException implements CodeNameException {
 
     private static final long serialVersionUID = 1957446799072827901L;
 
+    private Integer code;
+
+    private String name;
+
     public UnauthorizedException(String message) {
         super(message);
     }
@@ -15,4 +19,30 @@ public class UnauthorizedException extends RuntimeException {
         super(message, t);
     }
 
+    /**
+     * 带所有参数的构造方法
+     *
+     * @param code    - 数字标识,范围在100~999
+     * @param name    - 异常详细类型
+     * @param message - 文字提示
+     * @param cause   - 触发来源
+     */
+    public UnauthorizedException(Integer code, String name, String message, Throwable cause) {
+        super(message, cause);
+        if (code != null) {
+            if (code < 100 || code > 999) {
+                throw new IllegalArgumentException("UnauthorizedException.code should between 100~999");
+            }
+        }
+        this.code = code;
+        this.name = name;
+    }
+
+    public Integer getCode() {
+        return code;
+    }
+
+    public String getName() {
+        return name;
+    }
 }

+ 1 - 1
data-mybatis-plus/src/main/java/com/qmth/boot/mybatis/config/DataSourceProperties.java

@@ -45,7 +45,7 @@ public class DataSourceProperties {
 
     @NotNull
     @DurationMin(millis = 0)
-    private Duration maxLifetime = Duration.ofMillis(0);
+    private Duration maxLifetime = Duration.ofHours(7);
 
     @NotNull
     private String logLevel = LoggerConstant.DEFAULT_LEVEL;

+ 1 - 0
starter-api/src/main/java/com/qmth/boot/api/exception/DefaultExceptionEnum.java

@@ -10,6 +10,7 @@ public enum DefaultExceptionEnum {
 
     HEADER_INVALID(HttpStatus.BAD_REQUEST, 400001, "header field invalid"),
     REQUEST_PARAMETER_INVALID(HttpStatus.BAD_REQUEST, 400002, "request parameter invalid"),
+    ACCESS_FORBIDDEN(HttpStatus.FORBIDDEN, 403000, "access forbidden"),
     PLATFORM_FORBIDDEN(HttpStatus.FORBIDDEN, 403001, "platform not allowed"),
     IP_FORBIDDEN(HttpStatus.FORBIDDEN, 403002, "ip not allowed"),
     AUTHORIZATION_ERROR(HttpStatus.UNAUTHORIZED, 401001, "authorization error"),

+ 7 - 1
starter-api/src/main/java/com/qmth/boot/api/exception/GlobalExceptionHandler.java

@@ -34,7 +34,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
     @ExceptionHandler(UnauthorizedException.class)
     public ResponseEntity<Object> handleUnauthorizedException(UnauthorizedException e) {
         //log.error(e.getMessage(), e);
-        return handleApiException(DefaultExceptionEnum.AUTHORIZATION_ERROR.exception(e.getMessage()));
+        return handleApiException(DefaultExceptionEnum.AUTHORIZATION_ERROR.exception(e));
     }
 
     @ExceptionHandler(ReentrantException.class)
@@ -55,6 +55,12 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
         return handleApiException(DefaultExceptionEnum.RESOURCE_NOT_FOUND.exception(e));
     }
 
+    @ExceptionHandler(ForbiddenException.class)
+    public ResponseEntity<Object> handleForbiddenException(ForbiddenException e) {
+        //log.error(e.getMessage(), e);
+        return handleApiException(DefaultExceptionEnum.ACCESS_FORBIDDEN.exception(e));
+    }
+
     @ExceptionHandler(Exception.class)
     public ResponseEntity<Object> handleOtherException(Exception e) {
         log.error(e.getMessage(), e);