Joburgess преди 4 години
родител
ревизия
36d386eb8b

+ 5 - 4
mec-teacher/src/main/java/com/ym/mec/teacher/config/WebSocketConfig.java

@@ -1,14 +1,14 @@
 package com.ym.mec.teacher.config;
 
+import com.ym.mec.teacher.handler.CustomPrincipalHandshakeHandler;
 import com.ym.mec.teacher.handler.SoundHandler;
 import com.ym.mec.teacher.interceptor.WebSocketHandshakeInterceptor;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.messaging.simp.config.MessageBrokerRegistry;
-import org.springframework.web.socket.config.annotation.*;
-import org.springframework.web.socket.server.standard.ServerEndpointExporter;
-import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
+import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
+import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
+import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
 
 /**
  * @Author Joburgess
@@ -27,6 +27,7 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
     public void registerStompEndpoints(StompEndpointRegistry registry) {
         registry.addEndpoint("/soundWebSocket")
                 .addInterceptors(webSocketHandshakeInterceptor)
+                .setHandshakeHandler(new CustomPrincipalHandshakeHandler())
                 .setAllowedOrigins("*");
     }
 

+ 10 - 1
mec-teacher/src/main/java/com/ym/mec/teacher/controller/SoundController.java

@@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.messaging.handler.annotation.MessageMapping;
 import org.springframework.messaging.handler.annotation.SendTo;
+import org.springframework.messaging.simp.SimpMessagingTemplate;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -36,6 +37,8 @@ public class SoundController extends BaseController {
     private SoundService soundService;
     @Autowired
     private SysUserFeignService sysUserFeignService;
+    @Autowired
+    private SimpMessagingTemplate template;
 
     @ApiOperation(value = "评分")
     @PostMapping("compare")
@@ -53,7 +56,7 @@ public class SoundController extends BaseController {
         return soundService.measureCompare(musicXmlInfoList, record);
     }
 
-    @MessageMapping("/hello")
+    @MessageMapping("/soundDate")
     @SendTo("/topic/greetings")
     public String greeting(String message){
         SysUser sysUser = sysUserFeignService.queryUserInfo();
@@ -64,4 +67,10 @@ public class SoundController extends BaseController {
         return message;
     }
 
+    @RequestMapping("sendToUser")
+    public HttpResponseResult sendToUser(String phone, String message){
+        template.convertAndSendToUser(StringUtils.joinWith(":", "username", phone), "/topic/greetings", message);
+        return succeed();
+    }
+
 }

+ 63 - 0
mec-teacher/src/main/java/com/ym/mec/teacher/handler/CustomPrincipalHandshakeHandler.java

@@ -0,0 +1,63 @@
+package com.ym.mec.teacher.handler;
+
+import org.springframework.http.server.ServerHttpRequest;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.web.socket.WebSocketHandler;
+import org.springframework.web.socket.server.RequestUpgradeStrategy;
+import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
+
+import java.security.Principal;
+import java.util.Map;
+
+/**
+ * @Author Joburgess
+ * @Date 2021/6/15 0015
+ */
+public class CustomPrincipalHandshakeHandler extends DefaultHandshakeHandler {
+
+    public CustomPrincipalHandshakeHandler() {
+    }
+
+    public CustomPrincipalHandshakeHandler(RequestUpgradeStrategy requestUpgradeStrategy) {
+        super(requestUpgradeStrategy);
+    }
+
+    @Override
+    protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        CustomPrincipalPrincipal principal = new CustomPrincipalPrincipal();
+        principal.setName(authentication.getName());
+        return principal;
+    }
+
+    class CustomPrincipalPrincipal implements Principal {
+        private String name;
+
+        @Override
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+
+            CustomPrincipalPrincipal that = (CustomPrincipalPrincipal) o;
+
+            return name != null ? name.equals(that.name) : that.name == null;
+
+        }
+
+        @Override
+        public int hashCode() {
+            return name != null ? name.hashCode() : 0;
+        }
+    }
+
+}