To achieve the so-called stateless, using cookie authentication, do not use the session is every time to re-certification check the database?
My specific implementation is
Define a secret key key= "random";
Put a field in the User_profile table when token user registers, assign a random value
When the user logs in,
String usertoken= MD5 (user id+token+key);
Response.addcookie ("user_id", user ID);
Response.addcookie ("User_token", usertoken);
Then define a login blocker
String userId = Request.getcookie ("user_id");
String usertoken= Request.getcookie ("User_token");
String Token=db.getusertokenbyid (userId); We have to check the database every time.
String usertoken2= MD5 (user id+token+key);
if (Usertoken.equals (userToken2) {
Login successful
} else{
}
This is a simple way, but it is still effective in preventing counterfeit cookies.
But here every time to query the database, there is no better way to implement
Reply content:
To achieve the so-called stateless, using cookie authentication, do not use the session is every time to re-certification check the database?
My specific implementation is
Define a secret key key= "random";
Put a field in the User_profile table when token user registers, assign a random value
When the user logs in,
String usertoken= MD5 (user id+token+key);
Response.addcookie ("user_id", user ID);
Response.addcookie ("User_token", usertoken);
Then define a login blocker
String userId = Request.getcookie ("user_id");
String usertoken= Request.getcookie ("User_token");
String Token=db.getusertokenbyid (userId); We have to check the database every time.
String usertoken2= MD5 (user id+token+key);
if (Usertoken.equals (userToken2) {
Login successful
} else{
}
This is a simple way, but it is still effective in preventing counterfeit cookies.
But here every time to query the database, there is no better way to implement
Since the query is necessary for the whole transaction, it is necessary to consider improving the query efficiency, according to the actual demand, like Memcache,redis, the key-value pair form of data storage method is very important, both to meet the data storage requirements, but also to alleviate performance problems.
Do not need to query the database every time, token into memory to do the cache, the next time just check if Cache,token is valid, whether it expires on the line
Token: {
Uid:xxx
Expired:xxx
}
I don't know where this thing is better than the session. In fact, you can use your data to do AES encryption, and then the server side to do the reverse resolution.
Like what:
token = Aeshash (uid + ' | ' + mkey + ' + ' time)
Response.cookie (token, token);
Check Time
Uid,mkey,time = Deaes (token)
At the same time can also have Ip,ua, operating system what. As long as you like. can be verified according to their own situation, and then to verify the identity of the user.
database queries are unavoidable. There will always be such as user status, user identity, user rights and so a bunch of checks.
However, you need to complete the redis level, performance + persistence.
Do not save the database inside, with Memcache or Redis, both to solve the distributed problem and performance is also very high.
Can consider NoSQL