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)