标签特定的数据绑定到一个HTTP GET请求 [英] Binding tab-specific data to an HTTP GET request

查看:160
本文介绍了标签特定的数据绑定到一个HTTP GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个身份验证机制,每个浏览器标签页可以登录为不同的用户。

I'm trying to implement an authentication mechanism where each browser tab may be logged in as a different user.

下面是这个系统的规则:

Here are the rules of this system:

  • 在一个认证令牌指示哪个用户登录。
  • 有2种身份验证令牌:私人和公共
  • 在每间私人令牌绑定到一个单一的标签,决定了它的帐户信息。
  • 在公共标记可被读取/任何标签写入,并表示已登录到(所有选项卡)的最后一个帐户。
  • 当用户登录在任何选项卡中,无论是私人和公共标记都被删除。
  • 在每次选项卡点击一个网页,要求身份验证时,系统会尝试读取专用标记。如果它没有被设置(如在新的/空白标签的情况下),它试图拷贝到私有令牌公共标记的值。如果未设置公共标记,然后将用户重定向到一个验证屏幕。
  • 在当前已登录在标签和用户点击一个链接时,该请求必须包括在自定义HTTP标头的专用标记。在URI发送该信息是不是一种选择,出于安全原因。
  • 能够使用后退/前进按钮同样的导航,你会正常链接(意思是,没有任何提示重新提交表单数据)。

我试过到目前为止:

  • 使用饼干为私营和公共令牌:这不起作用,因为该服务器有没有办法知道看在其中的cookie如果一个。用户点击从选项卡中的链接,要求将所有的Cookie在所有选项卡和服务器有没有办法知道哪一个点击的链接中。

  • Using cookies for both the private and public tokens: this doesn't work because the server has no way of knowing which cookie to look in. If a user clicks on a link from inside a tab, the request sends all cookies across all tabs and the server has no way of knowing which one clicked on the link.

sessionStorage 存储专用标记:这不起作用,因为当用户点击一个链接,就没有办法来指定自定义标题,应该是发送旁边的HTTP GET请求。

Storing private tokens in sessionStorage: This doesn't work because when a user clicks on a link, there is no way to specify custom headers that should be sent alongside the HTTP GET request.

使用AJAX请求的页面,然后使用数据URI导航到该页面在内存:出于安全原因,Internet Explorer中不允许使用数据URI的HTML内容。请参阅<一href="http://msdn.microsoft.com/en-us/library/cc848897%28v=vs.85%29.aspx">http://msdn.microsoft.com/en-us/library/cc848897%28v=vs.85%29.aspx

Requesting the page using AJAX, then navigating to the page in memory using Data URIs: For security reasons, Internet Explorer doesn't allow the use of DATA URIs for HTML content. See http://msdn.microsoft.com/en-us/library/cc848897%28v=vs.85%29.aspx

使用&LT;形式方法=获得ENCTYPE =的multipart / form-data的&GT; 并使用隐藏域传送令牌:ENCTYPE =的multipart / form-data的仅支持POST。

Using <form method="get" enctype="multipart/form-data"> and passing the token using hidden fields: enctype="multipart/form-data" is only supported for POST.

使用&LT;形式方法=邮报是enctype =的multipart / form-data的&GT; ,并通过使用隐藏域令牌:从理论上讲,这应该工作,但现在被提示用户重新提交表单数据,如果他使用后退/前进按钮。

Using <form method="post" enctype="multipart/form-data"> and passing the token using hidden fields: in theory, this should work but now the user gets prompted to re-submit form data if he uses the back/forward button.

请求使用AJ​​AX,然后使用重写当前页面 document.open()的页面;文件撰写(); document.close()。我想这两个 http://stackoverflow.com/a/4404659/14731 和<一href="http://forums.mozillazine.org/viewtopic.php?p=5767285&sid=d6a5a2e8e311598cdbad124e277e0f52#p5767285">http://forums.mozillazine.org/viewtopic.php?p=5767285&sid=d6a5a2e8e311598cdbad124e277e0f52#p5767285在这两种情况下,在脚本中的新&LT; HEAD&GT; 块从来没有被执行

Requesting the page using AJAX, then rewriting the current page using document.open(); document.write(); document.close(). I tried both http://stackoverflow.com/a/4404659/14731 and http://forums.mozillazine.org/viewtopic.php?p=5767285&sid=d6a5a2e8e311598cdbad124e277e0f52#p5767285 and in both cases the scripts in the new <head> block never gets executed.

任何想法?

推荐答案

好了,经历许多不同的迭代后,这里是我们最终的实现:

Okay, after going through many different iterations, here is the implementation we ended up with:

  • 有两种类型的数据存储:
    • There are two kind of data stores:
      • IndexedDB, which is shared across all tabs.
      • sessionStorage which is unique per tab.
      • 索引型数据库中包含 publicToken nextTabId
      • sessionStorage包含 privateToken tabId
      • IndexedDB contains publicToken, nextTabId.
      • sessionStorage contains privateToken, tabId.
      • 查看 http://stackoverflow.com/a/1592572/14731 一个认证令牌的定义。
      • 有两种身份验证令牌:公共和私人
      • publicToken 是最后一次登录的操作返回,在所有选项卡中的令牌。
      • privateToken 是当前选项卡的最后一次登录的操作返回的标记。
      • See http://stackoverflow.com/a/1592572/14731 for a definition of an authentication token.
      • There are two kinds of authentication tokens: public and private.
      • publicToken is the token returned by the last login operation, across all tabs.
      • privateToken is the token returned by the last login operation of the current tab.
      • 在每个标签的唯一标识一个叫做标记 tabId
      • nextTabId 是一个数字,在所有选项卡进行访问。
      • 如果一个标签没有一个ID,它创建了一个基于 nextTabId 一个新的,并增加它的价值。
      • 例如, tabId 可以有com.company.Tab X ,其中<$ C的值$ C> X 是>按 nextTabId
      • Each tab is uniquely identified by a token called tabId.
      • nextTabId is a number that is accessible across all tabs.
      • If a tab does not have an id, it creates a new one based on nextTabId and increments its value.
      • For example, tabId could have a value of "com.company.TabX" where X is the number returned by nextTabId.
      • 在每次选项卡登录, privateToken publicToken 使用服务器返回的身份验证令牌将被覆盖。
      • 当用户注销时,我们删除 privateToken publicToken 在浏览器端和 privateToken 在服务器端。我们不会删除 publicToken 服务器端。
      • 这意味着任何时候一个选项卡注销,并共享同一个 privateToken 将得到注销以及所有选项卡。使用不同的令牌任何标签将不会受到影响。
      • 在什么时候多个选项卡共享同一 privateToken ?当你打开一个新的窗口或标签的链接,它继承了 privateToken 父标签。
      • 如果我们要删除 publicToken 在服务器上,一个标签注销与 privateToken X, publicToken Y就应该引起制表符 privateToken Y键获得注销(这是不可取的)。
      • Every time a tab logs in, privateToken and publicToken are overwritten using the authentication token returned by the server.
      • When a user logs out, we delete privateToken and publicToken on the browser side, and privateToken on the server side. We do not delete publicToken on the server side.
      • This means that anytime a tab logs out, all all tabs sharing the same privateToken will get logged out as well. Any tabs using a different token will be unaffected.
      • When do multiple tabs share the same privateToken? When you open a link in a new window or tab, it inherits the privateToken of the parent tab.
      • If we were to delete publicToken on the server, a tab logging out with privateToken X, publicToken Y would cause tabs with privateToken Y to get logged out (which is undesirable).
      • 扫描页面的HTML链接。
      • 对于每一个环节,追加 tabId 查询参数的URL。该参数值等于 tabId 的值。
      • 地带从当前页面使用 tabId URL参数<一href="https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history#The_replaceState()_method"相对=nofollow> history.replaceState(),以便用户可以与其好友分享链接( tabId 是用户特定的,不能共享)。
      • 删除 tabId 饼干(详见下文)。
      • Scan the page for HTML links.
      • For each link, append a tabId query parameter to the URL. The parameter value is equal to the value of tabId.
      • Strip the tabId URL parameter from the current page using history.replaceState() so users can share links with their friends (tabId is user-specific and cannot be shared).
      • Delete the tabId cookie (more on this below).
      • 在该选项卡中创建一个 tabId 饼干和遵循的链接。
      • 在这个cookie有一个名字等于 tabId 的值和值等于 privateToken <值/ LI>
      • The tab creates a tabId cookie and follows the link.
      • The cookie has a name equal to the value of tabId and a value equal to the value of privateToken
      • 如果在 tabId 参数丢失,然后重定向浏览器 GetTabId.html?引荐= X ,其中 X 是当前的URL。
      • 如果 tabId 是present而认证令牌无效或过期,那么浏览器重定向到登录界面。
      • If the tabId parameter is missing, then redirect the browser to GetTabId.html?referer=X where X is the current URL.
      • If tabId is present but the authentication token is invalid or expired, then redirect the browser to the login screen.
      • 如果标签没有一个 privateToken ,复制 publicToken privateToken
      • 如果两个 privateToken publicToken 是不确定的,重定向到登录页面。
      • 在该页面需要称为URL参数引用者它指示要重定向到成功。
      • 如果该选项卡中有一个 privateToken ,追加 tabId 参数添加到引用者页,并重定向到它。
      • 使用 window.location.replace()重定向删除时, GetTabId.html 从浏览器历史记录。
      • If the tab does not have a privateToken, copy publicToken into privateToken.
      • If both privateToken and publicToken are undefined, redirect to the login page.
      • The page takes a URL parameter called referer which indicates where to redirect to on success.
      • If the tab has a privateToken, append the tabId parameter to the referer page and redirect back to it.
      • Use window.location.replace() when redirecting to remove GetTabId.html from the browser history.
      • 我们试图尽量减少发送到服务器上的每个请求饼干数目。
      • 如果我们没有删除在页面加载的 tabId cookie,那么每次一个标签将作出请求中的所有其他选项卡中的Cookie会被发送为好。
      • We try to minimize the number of cookies sent to the server on each request.
      • If we did not delete the tabId cookie on page load, then each time a tab would make a request all of the other tabs' cookies would get sent as well.
      • 查看源文件打开缺少URL tabId 。作为结果,它得到的,而不是实际的网​​页会被重定向到 GetTabId.html 页面源 - code。
      • 在笨拙长的页面重新加载(客户端被重定向到 GetTabId.html 键,回到原来的页面)。
      • "View Source" opens the URL missing tabId. As result, it gets the source-code of the page which redirects to GetTabId.html instead of the actual page.
      • Awkwardly long page reloads (client is redirected to GetTabId.html and back to the original page).

      道歉长期实施的细节,但我无法找到一个更容易/更短的解决方案。

      Apologies for the long implementation details, but I could not find an easier/shorter solution.

      这篇关于标签特定的数据绑定到一个HTTP GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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