2

我正在做一個spring boot + oauth2 + websocket的例子。我在運行於8098端口的資源服務器上配置了spring websocket配置和其他代碼。我試圖從運行在8080端口上的客戶端應用程序連接它。但是當我運行我的應用程序時,我在瀏覽器控制檯上收到錯誤401(未授權)。我分享我的一些代碼:Spring + Oauth2 + JWT + Websocket

1)客戶端應用程序(在8080端口上運行)的JavaScript代碼來獲得WebSocket連接

'use strict'; 

// pull in the SockJS JavaScript library for talking over WebSockets. 
var SockJS = require('sockjs-client'); 
// pull in the stomp-websocket JavaScript library to use the STOMP sub-protocol. 
require('stompjs'); 

function register(registrations) { 
    const access_token = localStorage.getItem("ACCESS_TOKEN"); 
    console.log("Access Token " + access_token); 
    const csrf_token = localStorage.getItem("XSRF"); 
    // Here is where the WebSocket is pointed at the server application’s /messages endpoint 
    // websocket use this URL to open TCP connection 
    var socket = SockJS('http://localhost:8098/notifications'); 
    var stompClient = Stomp.over(socket); 

    var headers = { 
     'X-Csrf-Token': csrf_token, 
     Authorization: 'Bearer ' + access_token 
    } 

    // connect to server using stompClient 
    stompClient.connect(headers, function(frame) { 
     // Iterate over the array of registrations supplied so each can subscribe for callback as messages arrive. 
     stompClient.send("/app/notifications", {}); 
     registrations.forEach(function (registration) { 
      stompClient.subscribe(registration.route, registration.callback); 
     }); 
    }); 

} 

module.exports = { 
    register: register 
}; 

2)資源服務器(8098端口)代碼運行在那裏我有服務器側websocket配置

@Configuration 
@EnableWebSocketMessageBroker 
public class WebSocketConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer { 

    // This prefix we will append to every message's route. 
    static final String MESSAGE_PREFIX = "/topic" 
    static final String END_POINT = "/notifications" 
    static final String APPLICATION_DESTINATION_PREFIX = "/app" 

    @Override 
    protected boolean sameOriginDisabled() { 
     return true; 
    } 

    /** 
    * This method configure the end-point on the backend for clients and server to link ('/notifications'). 
    */ 
    @Override 
    public void registerStompEndpoints(StompEndpointRegistry registry) { 

     // This code register the '/notifications' end-point. The SockJS client will attempt to connect to '/notifications' end-point 
     if(registry != null) { 
      registry.addEndpoint(END_POINT).setAllowedOrigins("*").withSockJS() 
     } 

    } 

    /** 
    * This method configure the message broker used to relay messages between server and client. 
    */ 
    @Override 
    public void configureMessageBroker(MessageBrokerRegistry registry) { 

     if(registry != null) { 
      // Enable a broker to send messages to the client on destinations prefixed with '/topic' 
      registry.enableSimpleBroker(MESSAGE_PREFIX); 
      // prefix for messages that are bound for @MessageMapping annotated methods. This prefix will be used to define all message mappings 
      registry.setApplicationDestinationPrefixes(APPLICATION_DESTINATION_PREFIX) 
     } 
    } 
} 

請提出一些解決方案。

回答

3

最後我解決了這個問題。問題是我以前使用jwt_token和前綴'bearer'以解碼形式發送訪問令牌。

所以我做了一些調試,我開始知道該令牌應該以實際的形式發送,而不需要解碼。

  1. 以前我是在客戶端從這樣提取的響應訪問令牌:

令解碼= jwt_decode(response.entity.details.tokenValue); localStorage.setItem(「ACCESS_TOKEN」,decoded.jti);

這是不正確的。所以我改變它與下面的代碼:

localStorage.setItem(「ACCESS_TOKEN」,response.entity.details.tokenValue);

  • 在網頁套接字偵聽JS文件I下面做改變
  • 常量=的access_token localStorage.getItem( 「ACCESS_TOKEN」);

    var socket = SockJS('http://localhost:8098/notifications/?access_token='+ access_token);

    並且看到我在URL查詢參數中發送訪問令牌,但沒有前綴'bearer'。

    它工作。