Explorar o código

添加exception

Blumonilr %!s(int64=6) %!d(string=hai) anos
pai
achega
a78b41452c

+ 8 - 3
src/main/java/com/nju/edu/pay/web/AuthenticationInterceptor.java

@@ -2,6 +2,7 @@ package com.nju.edu.pay.web;
 
 import com.nju.edu.pay.util.jwt.JWTUtil;
 import com.nju.edu.pay.web.annotation.RootAllowed;
+import com.nju.edu.pay.web.exception.AuthException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.method.HandlerMethod;
 import org.springframework.web.servlet.HandlerInterceptor;
@@ -25,10 +26,14 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
         HandlerMethod handlerMethod=(HandlerMethod)object;
         Method method=handlerMethod.getMethod();
         if (method.isAnnotationPresent(RootAllowed.class)) {
-            RootAllowed passToken = method.getAnnotation(RootAllowed.class);
-            if (passToken.required()) {
+            RootAllowed rootAllowed = method.getAnnotation(RootAllowed.class);
+            if (rootAllowed.required()) {
                 String token=httpServletRequest.getHeader("Authorization");
-                return jwtUtil.verifyToken(token);
+                if(jwtUtil.verifyToken(token)){
+                    return true;
+                }else{
+                    throw new AuthException(401,"权限验证失败");
+                }
             }
         }
         return true;

+ 24 - 0
src/main/java/com/nju/edu/pay/web/exception/ApiExceptionHandler.java

@@ -0,0 +1,24 @@
+package com.nju.edu.pay.web.exception;
+
+import com.nju.edu.pay.web.response.ErrorResponse;
+import com.nju.edu.pay.web.response.Response;
+import org.apache.http.HttpStatus;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import javax.servlet.http.HttpServletResponse;
+
+@RestControllerAdvice({"com.nju.edu.pay.controller.api"})
+public class ApiExceptionHandler {
+	@ExceptionHandler(Exception.class)
+	@ResponseBody
+	public Response handle(HttpServletResponse response, Exception exception) {
+		int error = HttpStatus.SC_INTERNAL_SERVER_ERROR;
+		if (exception instanceof AuthException) {
+			error = ((AuthException) exception).getError();
+		}
+		response.setStatus(error);
+		return new ErrorResponse(error, exception.getMessage());
+	}
+}

+ 36 - 0
src/main/java/com/nju/edu/pay/web/exception/AuthException.java

@@ -0,0 +1,36 @@
+package com.nju.edu.pay.web.exception;
+
+import lombok.Getter;
+import lombok.ToString;
+import org.springframework.http.HttpStatus;
+
+@Getter
+@ToString
+public class AuthException extends Exception {
+    private int error;
+
+    public AuthException(){
+        this(HttpStatus.INTERNAL_SERVER_ERROR.value(),HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
+    }
+
+    public AuthException(int error) {
+        this(error, message(error));
+    }
+
+    public AuthException(String message) {
+        this(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
+    }
+
+    public AuthException(int error, String message) {
+        super(message);
+        this.error = error;
+    }
+
+    private static String message(int error) {
+        try {
+            return HttpStatus.valueOf(error).getReasonPhrase();
+        } catch (IllegalArgumentException e) {
+            return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();
+        }
+    }
+}