1018653686@qq.com 1 жил өмнө
parent
commit
d83d9676b8

+ 3 - 2
hx-xmhjc/src/main/java/com/fjhx/xmhjc/aspect/LoginValidAspect.java

@@ -6,6 +6,7 @@ import cn.hutool.extra.servlet.ServletUtil;
 import com.fjhx.xmhjc.anno.LoginValid;
 import com.fjhx.xmhjc.constants.LoginConstant;
 import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
+import com.fjhx.xmhjc.exception.LoginException;
 import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.common.utils.ServletUtils;
 import com.ruoyi.common.utils.spring.SpringUtils;
@@ -42,11 +43,11 @@ public class LoginValidAspect {
         });
         String token = request.getHeader(LoginConstant.LONGIN_HEAD);
         if (StrUtil.isBlank(token)) {
-            throw new RuntimeException("请先登录");
+            throw new LoginException("请先登录");
         }
         WebsiteUsers websiteUsers = redisCache.getCacheObject(LoginConstant.TOKEN_PREFIX+token);
         if (ObjectUtil.isNull(websiteUsers)) {
-            throw new RuntimeException("请先登录");
+            throw new LoginException("请先登录");
         }
     }
 

+ 9 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/exception/LoginException.java

@@ -0,0 +1,9 @@
+package com.fjhx.xmhjc.exception;
+
+public class LoginException extends RuntimeException {
+    private static final long serialVersionUID = 1L;
+
+    public LoginException(String message) {
+        super(message);
+    }
+}

+ 26 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/handle/LoginExceptionHandle.java

@@ -0,0 +1,26 @@
+package com.fjhx.xmhjc.handle;
+
+import com.fjhx.xmhjc.exception.LoginException;
+import com.ruoyi.common.core.domain.AjaxResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import javax.servlet.http.HttpServletRequest;
+
+@RestControllerAdvice
+public class LoginExceptionHandle {
+    private static final Logger log = LoggerFactory.getLogger(LoginExceptionHandle.class);
+
+
+    @ExceptionHandler(LoginException.class)
+    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
+    public AjaxResult handleLoginException(LoginException e, HttpServletRequest request) {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',发生登录异常.", requestURI, e);
+        return AjaxResult.error(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
+    }
+}