如何在每个会话的基础上限制高速公路python订阅

2024-10-06 12:28:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在服务器端使用带扭曲(wamp)的高速公路和浏览器中的高速公路。是否有一种直接的方法允许/限制每个会话的订阅?例如,客户机不应该订阅与其他用户相关的主题。在

当我不使用横杆io,我尝试使用本页末尾的“Example”部分中显示的Python代码,http://crossbar.io/docs/Authorization/首先使用RPC调用向客户机授予授权。当然,我使用我自己的授权逻辑。一旦此授权成功,我将授予客户端特权,以订阅仅与此客户端相关的主题,如'com.example.user用户名'. 但是,我的问题是,即使auth通过,我也没有找到一种方法来限制ApplicationSession类中的订阅请求,而ApplicationSession类是进行授权的地方。我如何防止授权给user_id=user_a的客户订阅'com.example.user\b'? 在


Tags: 方法用户iocom客户端主题客户机example
2条回答

您可以通过创建自己的路由器进行授权。为此,子类Router()并重写(至少)authorize()方法:

def authorize(self, session, uri, action):
    return True

这个方法非常简单,如果返回True,则会话被授权执行它正在尝试的任何操作。您可以制定一个规则,所有订阅都必须以'com.example.USER用户名,因此,python代码将拆分uri,获取第三个字段,并将其与当前会话id进行比较,如果匹配则返回True,否则返回false。这就是事情变得有点奇怪的地方。我有一个类似的代码,下面是我的authorize()方法:

^{pr2}$

请注意,我深入到会话中获取_authid,这是一种恶报(我认为),因为我不应该查看这些私有变量。不过,我不知道还能从哪儿弄到。在

另外,值得注意的是,这与身份验证密切相关。在我的实现中,authid是经过身份验证的用户id,它类似于unix用户id(正唯一整数)。我很确定这可以是任何东西,比如一个字符串,所以如果你愿意的话,你应该可以把你的'user'b'作为身份验证id。在

-克

我发现了一个使用Node-guest的相对简单的解决方案。代码如下:

    // crossbar setup
var autobahn = require('autobahn');

var connection = new autobahn.Connection({
        url: 'ws://127.0.0.1:8080/ws',
        realm: 'realm1'
    }
);

// Websocket to Scratch setup
// pull in the required node packages and assign variables for the entities
var WebSocketServer = require('websocket').server;
var http = require('http');

var ipPort = 1234; // ip port number for Scratch to use

// this connection is a crossbar connection
connection.onopen = function (session) {

    // create an http server that will be used to contain a WebSocket server
    var server = http.createServer(function (request, response) {
        // We are not processing any HTTP, so this is an empty function. 'server' is a wrapper for the
        // WebSocketServer we are going to create below.
    });

    // Create an IP listener using the http server
    server.listen(ipPort, function () {
        console.log('Webserver created and listening on port ' + ipPort);
    });

    // create the WebSocket Server and associate it with the httpServer
    var wsServer = new WebSocketServer({
        httpServer: server
    });

    // WebSocket server has been activated and a 'request' message has been received from client websocket
    wsServer.on('request', function (request) {
        // accept a connection request from Xi4S
        //myconnection is the WS connection to Scratch
        myconnection = request.accept(null, request.origin); // The server is now 'online'

        // Process Xi4S messages
        myconnection.on('message', function (message) {

            console.log('message received: ' + message.utf8Data);
            session.publish('com.serial.data', [message.utf8Data]);

            // Process each message type received
            myconnection.on('close', function (myconnection) {
                console.log('Client closed connection');
                boardReset();
            });
        });
    });
};

connection.open();

相关问题 更多 >