我有一个使用 Spring MVC 和 Security 的应用程序。我尝试向其中添加 Websocket。我已经成功连接,但是,当我尝试向后端发送消息时 - 什么也没发生。在调试模式下用@MessageMapping 注释的方法根本达不到!我不知道为什么。我已经尝试了很多谷歌解决方案,所以现在所有的配置都是下一个:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
public WebSocketConfig() {
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello1").withSockJS();
}
}
Run Code Online (Sandbox Code Playgroud)
我还为 websockets 添加了安全配置
@Configuration
public class SecuritySocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
protected boolean sameOriginDisabled() {
return true;
}
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages.simpDestMatchers("/hello1").authenticated().simpDestMatchers("/app/hello1").authenticated();//permitAll();
}
}
Run Code Online (Sandbox Code Playgroud)
控制器类
@Controller
public class WebsocketController {
@MessageMapping("/hello1")
public void send(Message message) {
String name = …Run Code Online (Sandbox Code Playgroud)