Realization of Login Account Single Browser Login in spring mvc Project in JSP Development

  • 2021-12-13 08:58:34
  • OfStack

Realization of login account single browser login in spring mvc project in JSP development

In many web products, it is necessary to realize that at the same time, only one account can be allowed to log in in one browser at the same time. Popularly speaking, when the A account is in
Login in Browser 1, then log in to A account in Browser 2. Then the A account in Browser 1 will be squeezed out. When the user manipulates the page in Browser 1, the page will

Skip to the login page and need to log in again. So how do we achieve this function? The following will give you a detailed introduction:

Principle

User A logs in in the browser with account a, and then user B logs in to account a in the browser on another computer. When user B login verification is successful, it will be touched
Send a login monitoring class. If it is judged in the monitoring class that the account a has been logged in by the user A, kick out the account a of the user A. At this time, when the user A operates the page, the page will jump to the login page.

Code implementation

In the implementation process, LoginListenner listening class, login login method and web. xml listening class configuration are used

LoginListenner

When the login is successful, the account object loginuser of the successful login is put into the session, and the attributeAdded event in the LoginListenner is triggered. In this event,
We judge whether there is session of the currently logged-in account in map where the corresponding relationship between account number and session is stored. If there is, we remove the session from map, log off the session at the same time, and then put the newly logged-in account number and session into map.

Here's the code:


/** 
 * 
 * @ClassName: LoginListenner 
 * @Description:  Login listener class - Treat the same 1 Time only allows account number, single place login  
 * @author mr_smile2014 605051929@qq.com 
 * @date 2014 Year 11 Month 12 Day   Afternoon 2:23:41 
 * 
 */ 
public class LoginListenner implements HttpSessionAttributeListener { 
  /** 
   *  Used to store account numbers and session Corresponding relationship map 
   */ 
  private Map<String, HttpSession> map = new HashMap<String, HttpSession>(); 
 
  /** 
   *  When direction session Triggered by putting data in  
   */ 
  public void attributeAdded(HttpSessionBindingEvent event) { 
    String name = event.getName(); 
 
    if (name.equals("loginuser")) { 
      User user = (User) event.getValue(); 
      if (map.get(user.getUserName()) != null) { 
        HttpSession session = map.get(user.getUserName()); 
        session.removeAttribute(user.getUserName()); 
        session.invalidate(); 
      } 
      map.put(user.getUserName(), event.getSession()); 
    } 
 
  } 
  /** 
   *  When direction session Remove data trigger in  
   */ 
  public void attributeRemoved(HttpSessionBindingEvent event) { 
    String name = event.getName(); 
 
    if (name.equals("loginuser")) { 
      User user = (User) event.getValue(); 
      map.remove(user.getUserName()); 
 
    } 
  } 
 
  public void attributeReplaced(HttpSessionBindingEvent event) { 
 
  } 
 
  public Map<String, HttpSession> getMap() { 
    return map; 
  } 
 
  public void setMap(Map<String, HttpSession> map) { 
    this.map = map; 
  } 
 
} 

Login method

Judge and verify the account number, password and verification code, and put the corresponding user object into session after passing the verification. The code is as follows:


/** 
   *  Login  
   * 
   * @param userName 
   * @param passWord 
   * @param code 
   *       Verification code  
   * @param type 
   *       Login type (merchant, operator)  
   * @param model 
   * @return 
   */ 
  @RequestMapping("/login") 
  public String login(String account, String passWord, String code, 
      Model model, HttpServletRequest request) { 
      // Login validates and returns the Login Successful User object  
      User user=loginResult(userPhone, passWord, code, request); 
      // Put the user object into the session Will trigger the LoginListenner In attributeAdded Events  
      request.getSession().setAttribute("loginuser", user); 
       
      } 

web. xml configuration

Configure the LoginListenner listening class into the web. xml file so that the listening for session will take effect. The configuration is as follows:


<!--1 Users can only be found in 1 Host login  --> 
  <listener> 
    <listener-class>com.test.listenner.LoginListenner</listener-class> 
  </listener> 

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: