如何阻止用户在 XPage 中打开新的浏览器会话 [英] How to stop a user from opening a new browser session in XPages

查看:31
本文介绍了如何阻止用户在 XPage 中打开新的浏览器会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个前端文档锁定过程,它创建一个应用程序范围变量,其中包含 UNID/用户名/时间,然后是一个每 30 秒更新一次此信息的计时器.如果有人尝试打开文档进行编辑,我会检查(使用 UNID)以查看其他人是否拥有该文档.如果时间大于 30 秒,我取消锁定并允许新用户打开它.

I have a front end document locking process that creates an Application Scope variable with the UNID/Username/time in it, then a timer that updates this information every 30 seconds. If someone tries opening the document to edit I check (using the UNID) to see if anyone else has the document. If the time is greater than 30 seconds I cancel the lock and allow the new user to open it.

我考虑过用数据库锁"做类似的事情,这很简单,但我需要浏览器会话的唯一标识符.我可以使用 ReplicaID/UserName/time 构建 applicationScope 变量,但我还需要一条信息来识别此浏览器会话.

I have considered do something similar with a database 'lock' which is pretty simple but I need a unique Identifier of the browser session. I can build the applicationScope variable using ReplicaID/UserName/time but I need one more piece of information that will identify this browser session.

在某处有这样的信息吗?类似浏览器 sessionID 的东西?

Is there such a piece of information available somewhere? Something like a browser sessionID?

推荐答案

您想只允许每个用户数据库一个打开浏览器窗口/标签.

You want to allow only one open browser window/tab per user and database.

对于您在浏览器中打开的每个 XPage,您必须测试相同的数据库是否已经在另一个浏览器窗口/选项卡中打开.如果是这样,请拒绝打开 XPage,例如重定向到错误 XPage.

For every XPage you open in browser you have to test if the same database is open in another browser window/tab already. If so, decline opening XPage with e.g. redirecting to an error XPage.

仅在服务器端执行此操作是不可能的,因为服务器不知道 Xpage 在哪个浏览器选项卡中打开.

It is impossible to do this on server side only as the server don't know in which browser tab an Xpage gets opened.

因此,服务器需要客户端的帮助.客户端必须给服务器一个唯一浏览器窗口/标签ID.

So, the server needs client's help. The client have to give the server a unique browser window/tab id.

浏览器窗口/标签没有唯一的 ID.但是,打开一个新窗口/标签,我们可以创建一个随机 ID 并将其存储在浏览器中sessionStorage.这是每个窗口/标签的唯一存储.

A browser window/tab doesn't have a unique id. But, opening a new window/tab we can create an random id and store it in browsers sessionStorage. This is a unique storage for every window/tab.

这个窗口/标签ID可以通过部分刷新GET发送到服务器,并在应用程序范围变量中为用户存储在服务器上.

This window/tab id can be send to server with a partial refresh GET and stored on server for the user in an application scope variable.

此外,服务器必须知道窗口/选项卡何时关闭,以便可以在另一个窗口/选项卡中打开相同的数据库.为此,客户端必须每 X 秒告诉服务器我还活着".

Also, the server has to know when a window/tab gets closed so that the same database can be opened in another window/tab. For this the client has to tell the server "I am alive" every X seconds.

这是完整的解决方案:

创建自定义控件UniqueBrowserTab"

Create a Custom Control "UniqueBrowserTab"

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:eventHandler
        event="onClientLoad"
        submit="false">
        <xp:this.script><![CDATA[
            var secondsIamAliveInterval = 5;
            var tabId = sessionStorage.getItem("tabId");
            if (!tabId) {
                tabId = Math.random();
                sessionStorage.setItem("tabId", tabId);
            }
            function sendTabIdToServer() {
                XSP.partialRefreshGet("#{id:browserTabControl}", {
                    params: {'tabId': tabId}
                });
            }
            sendTabIdToServer();
            setInterval(function() {
                sendTabIdToServer();
            }, secondsIamAliveInterval * 1000);
        ]]></xp:this.script>
    </xp:eventHandler>
    <xp:panel id="browserTabControl">
        <xp:this.rendered><![CDATA[#{javascript:
            var secondsIgnoreOtherSession = 7;
            if (param.tabId) {
                var userName = session.getEffectiveUserName();
                var userData = applicationScope.get(userName);
                var now = new Date().getTime();
                if (userData) {
                    if (userData.tabId !== param.tabId) {
                        if (userData.time + secondsIgnoreOtherSession*1000 > now) {
                            context.redirectToPage("Error.xsp");
                        }
                    }
                } 
                applicationScope.put(userName, {tabId : param.tabId, time: now});
            }
            return true
        }]]></xp:this.rendered>
    </xp:panel>
</xp:view>

将自定义控件UniqueBrowserTab"包含到每个 XPage 或例如进入应用程序布局的自定义控件.

Include the Custom Control "UniqueBrowserTab" into every XPages or e.g. into application layout's Custom Control.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core"
    xmlns:xc="http://www.ibm.com/xsp/custom">
    <xc:UniqueBrowserTab />
    ...
</xp:view>

创建一个 XPage Error.xsp,其中 包含自定义控件UniqueBrowserTab".

Create an XPage Error.xsp wich has not included the Custom Control "UniqueBrowserTab".

这将允许所有浏览器中的用户在每个数据库中只使用一个浏览器窗口/选项卡.

This will allow only one browser window/tab per database for a user across all browsers.

这篇关于如何阻止用户在 XPage 中打开新的浏览器会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆