首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring + Oauth2 + JWT+ Websocket

Spring + Oauth2 + JWT+ Websocket
EN

Stack Overflow用户
提问于 2017-08-07 19:02:16
回答 1查看 5.7K关注 0票数 5

我正在做一个关于spring + oauth2 + websocket的例子。在8098端口上运行的资源服务器上有spring配置和其他代码。我试图通过运行在8080端口上的客户端应用程序来连接它。但是当我运行我的应用程序时,我会在浏览器控制台上得到错误401 (未经授权)。我要分享我的一些代码:

1)客户端应用程序(运行在8080端口上) javaScript代码,以获得websocket连接。

代码语言:javascript
复制
'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配置。

代码语言:javascript
复制
@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)
        }
    }
}

请提出一些解决办法。

EN

回答 1

Stack Overflow用户

发布于 2017-08-11 14:22:25

最后我解决了这个问题。问题是以前我用jwt_token和前缀‘承载’以解码形式发送访问令牌。

所以我做了一些调试,我知道令牌应该以它的实际形式发送,而不需要解码。

  1. 以前,我在客户端从这样的响应中提取访问令牌:

let decoded = jwt_decode(response.entity.details.tokenValue); localStorage.setItem("ACCESS_TOKEN", decoded.jti);

这是不对的。所以我用下面的代码修改了它:

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

  1. 在websocket-listener js文件中,我做了以下更改
代码语言:javascript
复制
const access_token = localStorage.getItem("ACCESS_TOKEN");

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

并且看到我在没有前缀‘承载’的url查询参数中发送访问令牌。

而且起作用了。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45554010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档