开发者

Prevent user from opening JSF page in more than one tab/window in a browser

开发者 https://www.devze.com 2023-01-19 23:02 出处:网络
Is it possible 开发者_开发技巧to prevent user from opening JSF page in more than one browser tab or window? I agree with the accepted solution, but if you still have to do it, these steps worked for m

Is it possible 开发者_开发技巧to prevent user from opening JSF page in more than one browser tab or window?


I agree with the accepted solution, but if you still have to do it, these steps worked for me (pseudo code/pseudo python):

On the JS side:

if tabId not set:
  . generate random number 
  . set property in sessionStorage
otherwise:
  . get it from sessionStorage
make an ajax callback and send tabId
onError:
  . alert(); 
  . close current tab (if possible);
  . logout

Backend:(JSF):

Create custom filter:
  if request contains the tabId info:
    if it matches session's tabId: (being tabId not null)
      sent response status code to an error such as forbidden
      Optionally invalidate session
  otherwise apply session filtering (do nothing)


@BalusC The problem concerns old JSF application that is entirely stateful. Why try to figure out some way to inform users that openning the app in two separate tabs is potentially dangerous. In no way can we change session managed beans to request managed beans.

Make all those beans request scoped, install Tomahawk, add <t:saveState value="#{bean}" /> for every bean to every view of which you'd like to retain exactly the same bean state in the subsequent request. This works independently across tabs/windows.

Without Tomahawk, the alternative would be adding <h:inputHidden /> for every bean property which you'd like to retain in the subsequent request. I can however imagine that this may produce nasty boilerplate code when there are much or when they aren't of the standard EL types (for which you have thus to create a Converter). But that's what you get paid for.


One scenario I have in mind: Put a javascript component in the page that will constantly sending heartbeat to the server via AJAX. As long as there are heartbeat sent from browser, this page will be flagged as 'currently_viewing'. And as long as a page have that flag on, other requests to that page should be rejected.

The detail may be a lot messier than this simple story (e.g. you might need to have some sort of 'page id' to be sent with the heartbeat), but you get the idea (... i hope :).

Everything is possible as long as you are willing to pay the price.

0

精彩评论

暂无评论...
验证码 换一张
取 消