【问题标题】:Spring boot Websockets Handshake failed due to invalid Upgrade header: null由于无效的升级标头,Spring Boot Websockets 握手失败:null
【发布时间】:2021-11-11 05:17:38
【问题描述】:

我正在使用 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 帖子,几乎每个人都在使用代理服务器。我没有使用任何代理服务器。 (请让我知道我是否应该使用一个以及为什么)

代码sn-ps:

@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);
            }
        });
    }
}

【问题讨论】:

    标签: java spring-boot websocket stomp sockjs


    【解决方案1】:

    如果你不使用nginx代理,你应该配置spring boot支持https

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 2021-03-11
      • 2015-12-13
      • 2022-08-09
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多