首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring RSocket Security + RSocket-WebSocket-Client (浏览器)

Spring RSocket Security + RSocket-WebSocket-Client (浏览器)
EN

Stack Overflow用户
提问于 2021-03-30 23:36:20
回答 1查看 804关注 0票数 3

我正试图在Vue中创建一个站点,并在Spring上创建后端。我想使用rsocket来传输数据,但是一旦我在spring中添加了rsocket seurity,我就得到:

‘元数据格式错误’

想看看一个使用jwt/simpleauth的工作示例

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-02 23:09:56

我用简单的8月解决了这个问题,现在我想将这个授权与spring同步。那些。因此rsocket中的路由通过websecurity检查授权。我知道这可以通过jwt令牌实现,即通过rest将jwt令牌发送到客户端,但是如何在代码中做到这一点呢?JS客户端(浏览器)和Spring,我如何生成用户细节令牌?以防万一,我将留下一个简单实现的示例:

代码语言:javascript
复制
// METADATA BUILDER
    import {encodeRoute, encodeBearerAuthMetadata, encodeSimpleAuthMetadata, encodeAndAddCustomMetadata, encodeAndAddWellKnownMetadata, MESSAGE_RSOCKET_ROUTING, MESSAGE_RSOCKET_AUTHENTICATION} from "rsocket-core";
    
    export default class Metadata {
        constructor(json) {
            this.route = json['route'];
            this.auth = json['auth'];
        }
    
        toMetadata() {
            let metadata = Buffer.alloc(0);
            if (this.auth) {
                if (this.auth["type"] === 'bearer') {
                    metadata = encodeAndAddCustomMetadata(
                        metadata,
                        MESSAGE_RSOCKET_AUTHENTICATION.string,
                        encodeBearerAuthMetadata(this.auth["token"]),
                    );
                }
                if (this.auth["type"] === 'simple') {
                    metadata = encodeAndAddCustomMetadata(
                        metadata,
                        MESSAGE_RSOCKET_AUTHENTICATION.string,
                        encodeSimpleAuthMetadata(this.auth["username"], this.auth["password"]),
                    );
                }
            }
            if (this.route) {
                metadata = encodeAndAddWellKnownMetadata(
                    metadata,
                    MESSAGE_RSOCKET_ROUTING,
                    encodeRoute(this.route)
                );
            }
            return metadata;
        }
    }

// RSOCKET CLIENT CLASS
    import RSocketWebSocketClient from "rsocket-websocket-client";
    import {BufferEncoders, MESSAGE_RSOCKET_COMPOSITE_METADATA, RSocketClient,toBuffer} from "rsocket-core";
    import Metadata from "./metadata";
    
    
    export default class SpringClient {
        constructor(wsUrl, keepAlive = 60000, lifetime = 180000, dataMimeType = "application/json") {
            this.client = new RSocketClient({
                "setup": {
                    "keepAlive": keepAlive,
                    "lifetime": lifetime,
                    "dataMimeType": dataMimeType,
                    "metadataMimeType": MESSAGE_RSOCKET_COMPOSITE_METADATA.string
                },
                "transport": new RSocketWebSocketClient({
                    "url": wsUrl
                }, BufferEncoders)
            });
        }
    
        bearerAuth(token) {
            this.auth = {type: "bearer", token: token}
        }
    
        simpleAuth(username, password) {
            this.auth = {type: "simple", username: username, password: password}
        }
    
        logout() {
            this.auth = null;
        }
    
        connect(
            completeCallback = (socket) => {
            }, errorCallback = (error) => {
            }, subscribeCallback = (cancel) => {
            }
        ) {
            this.client.connect().subscribe({
                onComplete: socket => {
                    this.socket = socket;
                    completeCallback(socket);
                },
                onError: error => {
                    errorCallback(error);
                },
                onSubscribe: cancel => {
                    subscribeCallback(cancel);
                }
            });
        }
    
        requestResponse(data, route,
                        completeCallback = (data) => {
                        },
                        errorCallback = (error) => {
                        },
                        subscribeCallback = (cancel) => {
                        }
        ) {
            if (this.socket) {
                const metadata = new Metadata({
                    route: route,
                    auth: this.auth
                }).toMetadata();
                data = toBuffer(data);
                this.socket.requestResponse({
                    data,
                    metadata
                }).subscribe({
                    onComplete: data => {
                        completeCallback(data);
                    },
                    onError: error => {
                        errorCallback(error);
                    },
                    onSubscribe: cancel => {
                        subscribeCallback(cancel);
                    }
                });
            }
        }
    }
// EXAMPLE, HOW TO USE

    import SpringClient from "./springclient";
    
    this.client = new SpringClient("ws://localhost:7000/", 5000, 15000, "text/plain");
    this.client.connect(
        (socket) => {
            console.log("got connection complete");
            this.socket = socket;
        },
        (error) => {
            console.log("got connection error");
            console.error(error);
        },
        (cancel) => {
            console.log("got connection subscribe");
            /* call cancel() to abort */
        }
    )
    this.client.simpleAuth("LOGIN", "PASSWORD");
    this.client.requestResponse("MESSAGE", "ROUTE",
        (data) => {
            console.log("got response with requestResponse");
            console.log(data.data);
        },
        (error) => {
    
            console.log("got error with requestResponse");
            console.error(error);
        },
        (cancel) => {
            console.log(message);
            /* call cancel() to stop onComplete/onError */
        }
    );
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66879773

复制
相关文章

相似问题

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