Ensure that the same account can only be logged in on one device at the same time

Here we use redis to store the token


redis key design

access_token_{token}:{userInfoJsonStr}

    After the user logs in successfully, the token is used as the key, and the user information is used as the value. Function: userinfo can be found through token

token_by_userId_{userId}:{token}

    After the user logs in successfully, the user id is used as the key, and the token is used as the value: function: the token can be found through the userid
    Note: the expiration time of the two keys is the same

When the code logic
    user login authentication, through token_by_userId_{userId}, check whether the key exists in redis, if it exists, it means that the user has logged in. According to the found token, delete access_token_{token}, and then go to the login operation. (This process deletes the last login information, and the last login account will be offline.)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324934996&siteId=291194637