The application scope implements user login and squeezes out the previous login user code
1. application (ServletContext) is a scope saved on the server side. We save two forms of key-value pairs in the application: 1:, 2:
2. Whenever a user logs in (a new session will be generated), first query the sessionId in the application according to the userId:
If the sessionId is not queried, it means that there is no user logged in to this account, then save the two data to the application.
If the sessionId is queried, it means that a user has already logged in, then the following three steps will be performed:
1) First get the session that has been logged in and make it invalid
2) Then delete the original session from the application and save the new session to the application ()
3) Finally, delete the original sessionId from the application and save the new sessionId to application()
Second, the encoding of the handler that implements the login function
@RequestMapping("/login")
public String login(HttpServletRequest request, HttpServletResponse response, Map map) throws Exception{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
HttpSession session = request.getSession();
ServletContext application = session.getServletContext(); // application
User user = new User(userName, password);
User currentUser = userService.login(user);
if (currentUser == null) {
request.setAttribute("error", " ");
return "login";
}
String userId = String.valueOf(currentUser.getId()); // userId
map.put("currentUser", currentUser); // user session , @SessionAttributes
if(application.getAttribute("userId") == null){ //
application.setAttribute(userId, session.getId()); // sessionId application
application.setAttribute(session.getId(), session); // session application
}else{ //
String sessionId = (String) application.getAttribute(userId); // userId sessionId
HttpSession oldSession = (HttpSession) application.getAttribute(sessionId); // sessionId session
oldSession.invalidate(); // oldSession
application.removeAttribute(oldSession.getId()); // oldSession application
application.setAttribute(session.getId(), session); // session application
application.removeAttribute(userId); // oldSession id application
application.setAttribute(userId, session.getId()); // session Id application
}
return "main";
}
3. Summary
1. Please pay attention to why it is specially used to save the sessionId?
Because, when the second user logs in, if we want to invalidate the session of the first user, we must get the sessionId of the first user, so we need to save the sessionId in the form to find the first user through the userId. The sessionId of each user, so as to find the session of the first user and invalidate it
The above is the whole content of this article about the application scope to achieve user login and squeeze out the previous login user code, I hope it will be helpful to everyone. Interested friends can continue to refer to this site: Java thread lock object Lock-synchronization problem code example, share a simple java crawler framework, the usage code example of list in java collection, etc., if you have any questions, you can feel free to Leave a message, I will reply you in time. Thank you friends for your support of this site!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.