Forráskód Böngészése

架构模块 完善用户未注册异常捕获,增加用户未注册异常、用户名账号密码错误异常处理

songfuqiang 1 éve
szülő
commit
c0987969fe

+ 19 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/config/ControllerExceptionHandler.java

@@ -6,6 +6,8 @@ import exception.TooManyDeviceException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.support.DefaultMessageSourceResolvable;
+import org.springframework.security.authentication.BadCredentialsException;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.validation.BindingResult;
 import org.springframework.validation.FieldError;
 import org.springframework.web.bind.MethodArgumentNotValidException;
@@ -20,6 +22,9 @@ import javax.validation.ConstraintViolationException;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import static cn.kdan.pdf.backend.core.constant.AuthConstant.EXCEPTION_CODE_USER_NOT_FOUND;
+import static cn.kdan.pdf.backend.core.constant.AuthConstant.EXCEPTION_CODE_USER_PASSWORD_ERROR;
+
 /**
  * @author tangxiangan
  */
@@ -71,4 +76,18 @@ public class ControllerExceptionHandler {
         return new ResultMap<>(CommonConstant.EXCEPTION_CODE_SERVER_ERROR, "参数错误: " + e.getParameterName() + " 缺失");
     }
 
+    @ExceptionHandler(UsernameNotFoundException.class)
+    @ResponseBody
+    public ResultMap<?> handlerUsernameNotFoundException(UsernameNotFoundException e) {
+        logger.info(e.getMessage(), e);
+        return new ResultMap<>(EXCEPTION_CODE_USER_NOT_FOUND, "错误信息: " + e.getMessage());
+    }
+
+    @ExceptionHandler(BadCredentialsException.class)
+    @ResponseBody
+    public ResultMap<?> handlerBadCredentialsException(BadCredentialsException e) {
+        logger.info(e.getMessage(), e);
+        return new ResultMap<>(EXCEPTION_CODE_USER_PASSWORD_ERROR, "错误信息: " + e.getMessage());
+    }
+
 }

+ 1 - 1
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/AuthServiceImpl.java

@@ -357,7 +357,7 @@ public class AuthServiceImpl implements AuthService {
                         throw new BackendRuntimeException("账号/密码输入有误");
                     }
                 } else {
-                    throw new UsernameNotFoundException(JsonUtils.getJsonString(new ResultMap<>(AuthConstant.EXCEPTION_CODE_USER_NOT_FOUND, AuthConstant.EXCEPTION_MSG_USER_NOT_FOUND)));
+                    throw new UsernameNotFoundException(AuthConstant.EXCEPTION_MSG_USER_NOT_FOUND);
                 }
             }
         }