Session is stored in Cookie

By default, the Servlet container will automatically send a cookie named JSESSIONID to the client browser when creating a Session for the first time, which is used to uniquely identify the user's Session.

When the client sends subsequent requests, the browser will automatically append the cookie to the request header, allowing the Servlet container to identify and restore the Session associated with the user.

Example code

request.getSession().setAttribute("uid", uid);
request.getSession().setAttribute("user", userInfoResponse.toString());
request.getSession().setAttribute("access_token", access_token); 

In Java Servlet, request.getSession()used to obtain the Session object of the current request. When calling getSession()a method, the Servlet container will first check whether the request contains the Session ID (usually passed through a cookie). If so, the corresponding Session object will be returned; if not, a new Session object will be created and the Session ID will be returned. Sent to the cookie of the client's browser.

request.getSession().setAttribute()Methods are used to set properties in the Session object. In your code example, uid, , userInfoResponseand access_tokenwill be set as properties of Session. However, the settings of these properties will not directly cause Session data to be written to Cookie by default.

By default, the Servlet container will automatically send a cookie named JSESSIONID to the client browser when creating a Session for the first time, which is used to uniquely identify the user's Session. When the client sends subsequent requests, the browser will automatically append the cookie to the request header, allowing the Servlet container to identify and restore the Session associated with the user.

uidAs for other properties in Session (such as , userInfoResponseand in your code access_token), they are not automatically written to Cookie. They are only stored internally on the backend server and are associated by Session ID.

If you want to pass other attribute data to the front end (such as sending uid, , userInfoResponseand access_tokeninto the front-end JavaScript code), you need to put these attribute values ​​into the response's data as needed on the backend and pass it to the front end through the API response.

uidFor example, you can package , userInfoResponseand into a JSON object in the Spring Boot backend Controller access_token, and return the JSON object in the API response. Then, in your Vue.js front-end code, you can parse these property values ​​from the API response and use them in your front-end JavaScript.

Summary: Session ID is usually automatically managed by the Servlet container and passed to the client through Cookie. But other attribute values ​​​​in the Session are not automatically written to the Cookie. If you need to use these attribute values ​​​​in the front-end JavaScript, you need to pass them to the front-end through the API response.

Guess you like

Origin blog.csdn.net/qq_35930739/article/details/132082868