Spring Boot WebSockets 握手失败,因为无效的 Upgrade 标头: null。

5

我正在使用Spring Boot 2.1.6 RELEASE版本,尝试使用Stomp Websockets实现推送通知。我参考了这里的内容:https://github.com/netgloo/spring-boot-samples/tree/master/spring-boot-web-socket-user-notification

在本地使用时一切正常。但是当部署到使用HTTPS连接的服务器上时,日志中只能看到以下内容。

Handshake failed due to invalid Upgrade header: null

并在浏览器上执行

Websocket.js:6 WebSocket connection to 'wss://dev.myserver.in/ws/055/chbvjkl4/websocket' failed

我查阅了数十篇stackoverflow帖子,几乎所有人都在使用代理服务器。但我并没有使用任何代理服务器。(如果需要使用,请告知原因)

代码片段:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").withSockJS();
    }

}

以下是我目前允许 websocket 请求的方式

@Override
    public void configure(WebSecurity web) throws Exception {

        // Tell Spring to ignore securing the handshake endpoint. This allows the
        // handshake to take place unauthenticated
        web.ignoring().antMatchers("/ws/**");

    }

推送通知服务将在特定操作时被调用:

@Service
public class PushNotificationService {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    
    /**
     * Send notification to users subscribed on channel "/user/queue/notify".
     * The message will be sent only to the user with the given username.
     * 
     * @param notification The notification message.
     * @param username     The username for the user to send notification.
     */
    public void sendPushNotification(Notifications notification, String username) {
        simpMessagingTemplate.convertAndSendToUser(username, "/queue/notify", notification);
        return;
    }
}

在前端:

function connect() {
        // Create and init the SockJS object
        var socket = new SockJS('/ws');
        var stompClient = Stomp.over(socket);

        // Subscribe the '/notify' channel
        stompClient.connect({}, function(frame) {
          stompClient.subscribe('/user/queue/notify', function(notification) {
            notify(JSON.parse(notification.body));

          });
        });

这里是通知

function notify(message) {
    let notificationTitle = message.status;
    let body = message.createdOn;
    let link = message.url;
    if(Notification.permission === "granted") {
        showPushNotification(notificationTitle,body,link);
    }
    else {
        Notification.requestPermission(permission => {
            if(permission === 'granted') {
                showPushNotification(notificationTitle,body,link);
            }
        });
    }
}

你找到这个问题的解决方案了吗? - pacman
1个回答

0

如果您不使用像 nginx 代理,您应该配置 Spring Boot 支持 https


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接