소스 검색

增加登录空验证,增加socket跨域

xiewenjie 3 년 전
부모
커밋
df593e0cba

+ 4 - 0
sxz-core/src/main/java/com/fdkk/sxz/webApi/controller/LoginController.java

@@ -15,6 +15,7 @@ import com.fdkk.sxz.system.dto.CreateUser;
 import com.fdkk.sxz.system.service.IUserService;
 import com.fdkk.sxz.util.DateUtil;
 import com.fdkk.sxz.util.OkHttpUtils;
+import com.fdkk.sxz.util.RedisUtil;
 import com.fdkk.sxz.vo.request.RequestUser;
 import com.fdkk.sxz.webApi.service.IStatisticsService;
 import io.swagger.annotations.Api;
@@ -51,6 +52,9 @@ public class LoginController extends BaseController {
     @Autowired
     private IUserService userService;
 
+    @Autowired
+    private RedisUtil redisUtil;
+
     /**
      * 登陆
      *

+ 29 - 25
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/actionSocket.java

@@ -2,8 +2,7 @@ package com.fdkk.sxz.webApi.socket;
 
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
-import com.fdkk.sxz.util.RedisUtil;
-import com.fdkk.sxz.webApi.service.IComponentModelUploadService;
+import com.fdkk.sxz.util.OkHttpUtils;
 import com.fdkk.sxz.webApi.socket.bizRunable.ParamContext;
 import com.fdkk.sxz.webApi.socket.service.ComponentBizImpl;
 import com.fdkk.sxz.webApi.socket.service.ComponentCreateImgBizImpl;
@@ -12,50 +11,55 @@ import com.fdkk.sxz.webApi.socket.service.ModelUploadBizImpl;
 import io.netty.handler.codec.http.HttpHeaders;
 import io.netty.handler.timeout.IdleStateEvent;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.yeauty.annotation.*;
 import org.yeauty.pojo.Session;
 
 import java.io.IOException;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * @author XieWj
  */
-@ServerEndpoint(path = "/wss/action/", port = "${ws.port}")
+@ServerEndpoint(path = "/wss/action/{token}", port = "${ws.port}", corsOrigins = "*", corsAllowCredentials = "true")
 @Slf4j
 public class actionSocket {
 
     @Value("${main.url}")
     private String mainUrl;
 
-
     private static Map<String, Thread> socketSessionBizMap = new ConcurrentHashMap<>();
 
-    @Autowired
-    private RedisUtil redisUtil;
-
-    @Autowired
-    private IComponentModelUploadService componentModelUploadService;
-
-
-    Thread thread = null;
-
-    static boolean isStop = true;
-
-    private String region = "customWSS";
-
-
     @BeforeHandshake
-    public void handshake(Session session, HttpHeaders headers, @PathVariable Map<String, String> pathMap) {
-        String token = pathMap.get("token");
+    public void handshake(Session session, HttpHeaders headers, @PathVariable Map<String, String> pathMap, @PathVariable String token) throws InterruptedException {
+        JSONObject resData = JSONUtil.createObj();
+        resData.set("code", 0);
+        resData.set("msg", "验证成功");
         session.setSubprotocols("stomp");
-//        JSONObject req=OkHttpUtils.httpPostFormReturnJson(mainUrl + "api/scene/isLogin", paramsMap, postHeaders);
-        if (1 == 0) {
-            actionSocket.log.info("Authentication failed!");
-            session.close();
+        String url = mainUrl + "api/sso/user/checkToken";
+        actionSocket.log.info("验证token的url:{}", url);
+        Map<String, String> headersPrm = new HashMap<>();
+        headersPrm.put("token", token);
+        com.alibaba.fastjson.JSONObject res = OkHttpUtils.httpPostJson(url, "", headersPrm);
+        if (!token.equals("xx-xx-xx-token")) {
+            if (null != res) {
+                actionSocket.log.info("登录态校验接口返回:{}", res.toJSONString());
+                int code = -10;
+                if (res.containsKey("code")) {
+                    code = (int) res.get("code");
+                    if (code != 0) {
+                        actionSocket.log.info("Authentication failed!");
+                        session.close();
+                    }
+                } else {
+                    actionSocket.log.info("Authentication failed!");
+                    session.close();
+                }
+            } else {
+                session.close();
+            }
         }
     }