فهرست منبع

增加401 错误类型

吴洪双 5 سال پیش
والد
کامیت
6a88b58b71

+ 4 - 3
src/main/java/com/shawn/monitor/PerformanceMonitor.java

@@ -5,6 +5,7 @@ import com.shawn.model.entity.TAdmin;
 import com.shawn.service.interfac.TAdminServiceInterface;
 import com.shawn.util.JavaWebToken;
 import com.shawn.web.exception.MyException;
+import com.shawn.web.exception.UnLoginException;
 import lombok.extern.apachecommons.CommonsLog;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.StopWatch;
@@ -16,6 +17,7 @@ import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Pointcut;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Component;
 import org.springframework.web.context.request.RequestAttributes;
 import org.springframework.web.context.request.RequestContextHolder;
@@ -50,7 +52,6 @@ public class PerformanceMonitor {
         String token = null!=request.getHeader("token")?request.getHeader("token"):"";
         boolean isLogin = true;
         if(StringUtils.isEmpty(token)){
-            // TODO 做token的逻辑判断
             isLogin = false;
         }else{
             Map<String, Object> map = JavaWebToken.parserJavaWebToken(token);
@@ -64,9 +65,9 @@ public class PerformanceMonitor {
             }
         }
         if(!isLogin){
-            throw new MyException("请登录");
+            throw new UnLoginException("请登录");
         }
-//        throw new MyException("请登录");
+//         throw new UnLoginException("请登录");
     }
 
 }

+ 11 - 2
src/main/java/com/shawn/web/exception/ExceptionHandlerControllerAdvice.java

@@ -19,7 +19,16 @@ import javax.servlet.http.HttpServletRequest;
 @Slf4j
 @ControllerAdvice
 class ExceptionHandlerControllerAdvice {
-	
+    @ExceptionHandler(UnLoginException.class)
+    public ResponseEntity<?> UnLoginExceptionHandler(HttpServletRequest request, UnLoginException e) {
+        logError(request, e);
+
+        return ResponseEntity
+                .status(HttpStatus.UNAUTHORIZED)
+                .body(new ResultMessage()
+                        .setCode(false)
+                        .setMessage(e.getMessage()));
+    }
 	@ExceptionHandler(MyException.class)
 	public ResponseEntity<?> MyExceptionHandler(HttpServletRequest request, MyException e) {
 		logError(request, e);
@@ -28,7 +37,7 @@ class ExceptionHandlerControllerAdvice {
 				.status(HttpStatus.OK)
 				.body(new ResultMessage()
 						.setCode(false)
-						.setMessage(e.getMessage())); 
+						.setMessage(e.getMessage()));
 	}
     @ExceptionHandler(ParamNotFoundException.class)
     public ResponseEntity<?> paramNotFoundExceptionHandler(HttpServletRequest request, ParamNotFoundException e) {

+ 22 - 0
src/main/java/com/shawn/web/exception/UnLoginException.java

@@ -0,0 +1,22 @@
+package com.shawn.web.exception;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.experimental.Accessors;
+
+/**
+ * 未登录
+ * @author 吴洪双
+ */
+@Accessors(chain = true)
+@Setter
+@Getter
+public class UnLoginException extends RuntimeException {
+
+    private static final long serialVersionUID = -2565431806475335332L;
+    String message;
+
+    public UnLoginException(String message){
+        this.message = message;
+    }
+}