Przeglądaj źródła

feat: 验证code

mjh 4 lat temu
rodzic
commit
6d2b5e081a

+ 31 - 18
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/web/rest/UserJWTController.java

@@ -9,10 +9,13 @@ import cn.seecoder.seecoderportalserver.mapper.UserMapper;
 import cn.seecoder.seecoderportalserver.security.DomainUserDetails;
 import cn.seecoder.seecoderportalserver.security.SecurityUtils;
 import cn.seecoder.seecoderportalserver.security.jwt.TokenProvider;
+import cn.seecoder.seecoderportalserver.service.HttpService;
 import cn.seecoder.seecoderportalserver.service.SuperService;
 import cn.seecoder.seecoderportalserver.service.UserService;
 import cn.seecoder.seecoderportalserver.utility.OkResponse;
 import cn.seecoder.seecoderportalserver.web.rest.errors.CustomErrorException;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.security.authentication.InternalAuthenticationServiceException;
@@ -32,31 +35,25 @@ import javax.validation.Valid;
 @RestController
 @RequestMapping("/api")
 public class UserJWTController {
-
     private static final Logger log = LoggerFactory.getLogger(UserJWTController.class);
-
     private static final int ONE_DAY = 86400;
-
     private static final int THIRTY_DAY = 2592000;
-
     private final TokenProvider tokenProvider;
-
     private final AuthenticationManagerBuilder authenticationManagerBuilder;
-
     private final UserService userService;
-
     private final UserMapper userMapper;
-
     private final SuperService superService;
+    private final HttpService httpService;
 
     public UserJWTController(
             TokenProvider tokenProvider,
-            AuthenticationManagerBuilder authenticationManagerBuilder, UserService userService, UserMapper userMapper, SuperService superService) {
+            AuthenticationManagerBuilder authenticationManagerBuilder, UserService userService, UserMapper userMapper, SuperService superService, HttpService httpService) {
         this.tokenProvider = tokenProvider;
         this.authenticationManagerBuilder = authenticationManagerBuilder;
         this.userService = userService;
         this.userMapper = userMapper;
         this.superService = superService;
+        this.httpService = httpService;
     }
 
     @PostMapping("/user/login")
@@ -90,14 +87,30 @@ public class UserJWTController {
     }
 
     @PostMapping("/user/login/platform")
-    public OkResponse<TokenVO> authorizePlatformUser(@RequestBody ValidateCodeDTO validateCodeDTO) {
-        // todo: 验证code
-        String jwt = tokenProvider.createPlatformToken(validateCodeDTO.getUserId());
-        TokenVO tokenVO = new TokenVO();
-        tokenVO.setToken(jwt);
-        UserVO userVO = userService.findUserByUsername(validateCodeDTO.getUsername());
-        tokenVO.setName(userVO.getName());
-        tokenVO.setUser(userVO);
-        return OkResponse.of(tokenVO);
+    public OkResponse<TokenVO> authorizePlatformUser(@RequestBody ValidateCodeDTO validateCodeDTO) throws CustomErrorException{
+        JSONObject requestBody = new JSONObject();
+        requestBody.put("userId", validateCodeDTO.getUserId());
+        requestBody.put("code", validateCodeDTO.getCode());
+        JSONObject httpResult;
+        try {
+            httpResult = JSON.parseObject(
+                    httpService.sendPost("https://www.yuantusoft.com:8600/user/api/v1/external/verify/code",
+                            requestBody, null, null)
+            );
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        String[] keys = new String[]{"res", "code"};
+        if (httpResult.getInteger(keys[1]) == 0) {
+            String jwt = tokenProvider.createPlatformToken(validateCodeDTO.getUserId());
+            TokenVO tokenVO = new TokenVO();
+            tokenVO.setToken(jwt);
+            UserVO userVO = userService.findUserByUsername(validateCodeDTO.getUsername());
+            tokenVO.setName(userVO.getName());
+            tokenVO.setUser(userVO);
+            return OkResponse.of(tokenVO);
+        } else {
+            throw new CustomErrorException(httpResult.getString(keys[0]));
+        }
     }
 }