struts2+spring prevent users from repeating logins (single sign-on)

Source: Internet
Author: User

Everyone knows that the user login session is a good choice, and almost all of the session is used, but the session can not be used across windows, each open a browser, the system will give a new SessionID, and each sessionid is different, This means that each time you log in with a different session, rather than the one you used to log in. So what if you want to use only one user ID in the whole domain?

We can use this sessionlistener to implement Httpsessionattributelistener by customizing a Sessionlistener, implementing three methods to implement the interface, attributeadded, Attributeremoved, attributereplaced. Here we only need to implement the first two methods. First, we use a list container to install the currently logged-in user, and then when other users log in to determine whether the user has been logged in, if the user has logged in, you can choose the friendly prompt that the user has logged in, you can also directly to the user who has logged in to the current user.

The specific code is as follows:


public class Sessionlistener implements httpsessionattributelistener{public static List List = new ArrayList ();  public void attributeadded (Httpsessionbindingevent arg0) {//TODO auto-generated method Stubif (Arg0.getname (). Equals (" User ") | | Arg0.getname (). Equals ("Backuser")) {User user= (user) Arg0.getvalue (); List.add (user);}} public void attributeremoved (Httpsessionbindingevent arg0) {//TODO auto-generated Method stub try {              int n = 0;              User U = (user) arg0.getvalue ();              for (int i = 0; i < list.size (); i++) {                  User user = (user) list.get (i);                  if (U.getid (). Equals (User.getid ())) {                      n = i;                      break;                  }              }              List.remove (n);          } catch (Exception e) {          }  }public void attributereplaced (Httpsessionbindingevent arg0) {//TODO auto-generated Method stub}}

To determine whether a user repeats the logon method:

Whether the closed method of duplicate logons public      static Boolean getrepeat (Long userId) {          Boolean flag = false;          List List = Sessionlistener.list;          for (int i = 0; i < list.size (); i++) {              User user = (user) (List.get (i));                       if (Userid.equals (User.getid ())) {                  flag = true;              }          }          return flag;      }  


struts2+spring prevent users from repeating logins (single sign-on)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.