The idea of implementing a simple single-user login function with Java

Source: Internet
Author: User

Reference

The so-called "single user single account login" means: In the same system, a user name can not be logged in two places simultaneously.

We refer to QQ to achieve the effect: When an account is logged in a, in the case of no exit, if you go to the B login, then the system will be squeezed under a login account.

To implement such a feature, if our project uses SPRINGMVC we can use interceptors instead of filters, otherwise we can use filters to implement, we need one such thing to intercept the user's request. Our user table also needs to add a token field that holds a unique identifier for the current login.

The general idea is this:

User A request 123 This account login, log in successfully generated a unique token to deposit 123 of this object token, and put this user object in session, the most important thing is not to forget to update the user's table records. If at this time User B also requests to login 123 account, the same we also generated after the successful login to the unique token to deposit 123 of the token, and put this user object in session, the most important thing is not to forget to update the user's table records. Now the situation is that two users in two locations logged in the same account 123, then if user A to the server sent a request he wants to see the personal data, our interceptor will start to work, it can be said that the entire single-user landing is the focus of the interceptor inside, In the interceptor we go to the user object saved in the session, at this time we have to understand that the current user object token is User A, and the database of real token data has been changed after User B login, marked by User B, According to this condition, we use the user object in the session and the user in the database to make a comparison to determine if the token is the same as the single-user login, if the same description is the same user, otherwise it means that there are other people landed in other places, Need to clear out the current session of the user to exit the current account can be.

The idea of implementing a simple single-user login function with Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.