JavaWeb can only log in at one place at a time with the same account (similar to QQ login function)

JavaWeb realizes that one account can only log in at one place at a time (similar to QQ login function). The implementation idea is as follows:

1、 What does this function do

Let's think about it. There will always be such a demand anyway. There will be no demand these days.. ha-ha. Sometimes it's not necessarily a demand. It's likely to do the same for safety. For example, the examination system and online chat system are necessary.

2、 Implementation process

a. Problem analysis

In the system, we usually bind the login information to the session. It seems that it is possible to find a solution from this point of view. To put it bluntly, when a user logs in, judge whether the user has logged in. If so, clear the previous session.. It seems simple, doesn't it? In fact, if you think about it carefully, you will find the following problems: how to get whether the user has logged in before, that is, how to access all the logged in session information?

b. Concrete implementation

As we all know, there seems to be no specific method to directly obtain all session information in J2EE API. However, we can configure listeners to monitor the creation and destruction of all sessions, as well as the creation, deletion and replacement of attributes in sessions.

In fact, we only need to do the following:

When saving the user login information to the session, the corresponding process is the creation process of an attribute of the session (attributeadded). The current session can be recorded in an ArrayList.

In fact, when saving to the list, you should first traverse whether the user's login information already exists in the list. If it exists, destroy the session information in the list and remove it from the list. If it does not exist, put the session information in the list.

When the login information of a session is destroyed, the session is directly removed from the list.

There is also a process of replacing the session attribute when the user does not log out after logging in. Processing is also required to determine whether the new user already exists in other sessions other than the current session. Delete if it exists.

The specific codes are as follows:

On the web Configuration in XML

3、 Possible problems

The whole program may be a little unexpected. There may be some bugs, which should be used carefully for specific projects. You are welcome to make bricks and give some suggestions. I'll improve.

4、 Some later thoughts

If two machines use the same account to log in to the system at the same time, can both accounts log in successfully.. (in addition, when the session list is large, two machines may log in to the system with the same account at the same time during the traversal period). It is very tangled. How should we control it?

(solution: after testing, listener is a singleton in the system. Adding the synchronize keyword to its method can ensure the thread safety of the list.)

The above is what Xiaobian introduced to you. Java Web can only log in at one place at a time with the same account (similar to QQ login function). I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>