瀏覽代碼

fix: 登录时找不到手机号时不再显示"no value present"而是显示正确的报错信息。

mjh 4 年之前
父節點
當前提交
aea50eb157

+ 8 - 2
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/web/rest/UserJWTController.java

@@ -10,8 +10,10 @@ import cn.seecoder.seecoderportalserver.security.SecurityUtils;
 import cn.seecoder.seecoderportalserver.security.jwt.TokenProvider;
 import cn.seecoder.seecoderportalserver.service.SuperService;
 import cn.seecoder.seecoderportalserver.utility.OkResponse;
+import cn.seecoder.seecoderportalserver.web.rest.errors.CustomErrorException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.security.authentication.InternalAuthenticationServiceException;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 import org.springframework.security.core.Authentication;
@@ -60,8 +62,12 @@ public class UserJWTController {
                 new UsernamePasswordAuthenticationToken(
                         loginVO.getPhone(), loginVO.getPassword());
 
-        Authentication authentication =
-                authenticationManagerBuilder.getObject().authenticate(authenticationToken);
+        Authentication authentication;
+        try {
+            authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
+        }catch (InternalAuthenticationServiceException exception) {
+            throw CustomErrorException.userPhoneNotFound(loginVO.getPhone());
+        }
         SecurityContextHolder.getContext().setAuthentication(authentication);
         String jwt = tokenProvider.createToken(authentication);
 

+ 9 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/web/rest/errors/CustomErrorException.java

@@ -57,4 +57,13 @@ public class CustomErrorException extends RuntimeException {
     public static CustomErrorException devcloudError(String info) {
         return new CustomErrorException("devcloud: " + info);
     }
+
+    /**
+     * 手机号不存在触发报错
+     * @param phone 手机号
+     * @return 抛出异常
+     */
+    public static CustomErrorException userPhoneNotFound(String phone){
+        return new CustomErrorException("手机号 \"" + phone + " \"无效!");
+    }
 }