As we all know, a small partner who has been a web developer probably knows that when a browser sends a request to the server, the server creates a session for the current visitor, and the session ends as the browser closes. But the mobile client is all right (ios/android). I have studied a few, found that ios/android with the original interface to send the maximum drop feature is every time a session, so the login function is basically obsolete. The meaning of the login function is to store the information of the user authentication success in the session, the result is not OK to request a new session every time.
Then how to ensure that the client's logon session created in the subsequent interface requests can be effective for the client to provide session operations, such as the storage of user information entities, User Rights function menu storage what. First of all, a concept of popular science: Cookies, do not understand their own Baidu, there is a more popular mobile development concept Access_token, did the development should understand, do not understand their own Baidu.
The browser is also based on cookies, which explains why the website needs to be re-logged every time the cookie is emptied. When the browser accesses the server, the server contains the cookie information in the response message, the cookie is SessionID, and the SessionID is cached by the browser in the local cookie. Subsequent visits to all requests for the site are automatically accompanied by cookie information in the header header of the HTTP request. Therefore, the server uses the SessionID in the cookie to determine the identity of the current visitor and to match the session object of the current SessionID from the session collection.
Besides, another concept Access_token tokens, this is the contract, according to the development needs. For example, there is a scenario: User logon requests, when the user logs on, with the user name name and password pwd to access the login interface. If the server confirms that the user is legitimate, then we should generate a access_token from the server, save it on the server session, and return the Access_token to the client.
OK, here, the client obtains a total of two things, including the SessionID cookie and Access_token, with these two things, the client subsequently on the request server on any interface, need to carry access_token and cookies. The server will identify which session you belong to by using cookies, determine whether you are legal by Access_token, and whether the login expires (the service side has the final say). On the code:
////////////////////////////////////////////////////////////I. Create a session at login and return Access_token to the client///////////////////////////////////////////////////////////Access_token token as login status identificationHttpServletRequest req =Mvcs.getreq (); HttpSession Session= Req.getsession (true);//Create a session//UUID as tokenString Access_token =Uuid.randomuuid (). toString (); Session.setattribute ("Access_token", Access_token);//out.print ("Access_token:" +access_token);////////////////////////////////////////////////////////////two. Subsequent interface access in the server-side filter chain logic/////////////////////////////////////////////////////////HttpServletRequest request =Mvcs.getreq (); HttpSession HttpSession= Request.getsession (true); String Rat= Request.getparameter ("Access_token"); String Sat= (String) httpsession.getattribute ("Access_token");if(rat!=NULL&&rat.equals (Sat)) {//if equal, the description Access_token is valid. //release ... }
Note: As long as you submit a shape like: cookie:jsessionid=16e11471753944cf4f68a9f665e9b97f key value pair in the HTTP request header, The server automatically matches the session that belongs to this ID in the service with the SessionID contained in the cookie and provides the service.
Mobile client and server session that secret.