Browse Source

自定义登录失败异常类

hurugang 6 years ago
parent
commit
b980a11afa

+ 24 - 0
src/main/java/cn/hnthyy/thmz/entity/AuthException.java

@@ -0,0 +1,24 @@
+package cn.hnthyy.thmz.entity;
+
+/**
+ * 自定义的登录校验异常
+ */
+public class AuthException extends Exception {
+    private int value;
+    public AuthException() {
+        super();
+    }
+    public AuthException(String msg) {
+        super(msg);
+        this.value=-1;
+    }
+    public AuthException(int value, String msg) {
+        super(msg);
+        this.value=value;
+    }
+    public int getValue() {
+        return value;
+    }
+    
+    
+}

+ 5 - 4
src/main/java/cn/hnthyy/thmz/interceptor/AuthenticationInterceptor.java

@@ -4,6 +4,7 @@ import cn.hnthyy.thmz.Utils.TokenUtil;
 import cn.hnthyy.thmz.comment.PassToken;
 import cn.hnthyy.thmz.comment.UserLoginToken;
 import cn.hnthyy.thmz.common.Constants;
+import cn.hnthyy.thmz.entity.AuthException;
 import cn.hnthyy.thmz.entity.thmz.Token;
 import cn.hnthyy.thmz.service.thmz.TokenService;
 import com.auth0.jwt.JWT;
@@ -27,7 +28,7 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
     private TokenService tokenService;
 
     @Override
-    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) {
+    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws AuthException {
         // 从 http 请求头中取出 token
         String token = TokenUtil.getToken(httpServletRequest);
         // 如果不是映射到方法直接通过
@@ -47,12 +48,12 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
         if (method.isAnnotationPresent(UserLoginToken.class)) {
             // 执行认证
             if (token == null) {
-                throw new RuntimeException("401");
+                throw new AuthException("401");
             }
             Token tokenObject = tokenService.queryFromCache(token);
             if (tokenObject != null) {
                 //token已经失效,实际是退出登录主动失效了token
-                throw new RuntimeException("401");
+                throw new AuthException("401");
             }
             // 验证 token
             JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(Constants.JWT_SECRET)).build();
@@ -60,7 +61,7 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
                 jwtVerifier.verify(token);
             } catch (JWTVerificationException e) {
                 //token超时
-                throw new RuntimeException("401");
+                throw new AuthException("401");
             }
             return true;
         }

+ 3 - 2
src/main/java/cn/hnthyy/thmz/interceptor/GlobalExceptionHandler.java

@@ -1,6 +1,7 @@
 package cn.hnthyy.thmz.interceptor;
 
 
+import cn.hnthyy.thmz.entity.AuthException;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.http.MediaType;
@@ -19,8 +20,8 @@ import java.io.PrintWriter;
 @ControllerAdvice
 public class GlobalExceptionHandler {
 
-    @ExceptionHandler(value = Exception.class)
-    public ModelAndView defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){
+    @ExceptionHandler(value = AuthException.class)
+    public ModelAndView defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Object handler, AuthException ex){
         response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
         System.out.println("=====================全局异常信息捕获=======================");
         String  msg= ex.getMessage();