First create an online user collection class:
public class Onlineusermap {public
static map<string, UserInfo > onlineuser = new hashmap<string, UserInfo > ();
/**
* Get online user
* @return
/public static map<string, UserInfo > Getonlineuser () {
return Onlineuser;
}
/**
* Add Online user
* @param sessionId
* @param userName
* @return
/public void Addonlineuser ( String userId, UserInfo UserInfo) {
onlineuser.put (userId, UserInfo);
}
/**
* Remove user
* @param userName */public
void Removeuser (String sessionId) {for
string userId: Onlineuser.keyset ()) {
if (Onlineuser.get (userId). GetSessionID (). Equals (SessionId)) {
Onlineuser.remove (userId);
Break;}}}
Then configure the session listener (XML file configuration omitted):
public class Userloginlistener implements Httpsessionattributelistener {@Override public void attributeadded (HTT
Psessionbindingevent se) {//TODO auto-generated method stub String username = Se.getname ();
String sessionId = Se.getsession (). GetId (); if (username = = baseaction.session_user_info) {UserInfo UserInfo = (UserInfo) se.getsession (). getattribute (Base
Action.session_user_info);
New Onlineusermap (). Addonlineuser (Userinfo.getuserid (). toString (), userInfo); @Override public void attributeremoved (httpsessionbindingevent se) {//TODO auto-generated Metho
D stub String username = Se.getname ();
String sessionId = Se.getsession (). GetId ();
if (username = = baseaction.session_user_info) {new Onlineusermap (). Removeuser (SessionId); @Override public void attributereplaced (httpsessionbindingevent se) {//TODO auto-generated MethOD stub String username = Se.getname ();
String sessionId = Se.getsession (). GetId (); if (username = = baseaction.session_user_info) {UserInfo UserInfo = (UserInfo) se.getsession (). getattribute (Base
Action.session_user_info);
New Onlineusermap (). Addonlineuser (Userinfo.getuserid (). toString (), userInfo); }
}
}
Configure a custom interceptor (XML file configuration omitted) to determine the current logged in interceptor (multiple login restrictions for the same account):
public class Custominterceptor implements interceptor {private static final long serialversionuid = 1L; public void Destroy () {} public void Init () {} public String intercept (actioninvocation invocation) Throws Exception {if (invocation.getaction () instanceof baseaction) {baseaction baseaction = (baseac
tion) invocation.getaction ();
Userinfo Userinfo = (Userinfo) getsession (). getattribute (Session_user_info); if (UserInfo = = null) throw new Notlogedinexception ();//not logged in UserInfo Loginuserinfo = new Onli Neusermap (). Onlineuser.get (Userinfo.getuserid ());//Current login Session/user if (!loginuserinfo.getsessionid (). Equals (g
Etsession (). GetId ())) {//user lands getsession () in another place (). setattribute (Session_user_info, NULL); GetSession (). setattribute (Kickout_message, "user" + userinfo.getname () + "Already at address '" + loginuserinfo.getip () + "' re-login, when Before landing invalid.
");throw new Notlogedinexception ()//Not logged in}} else {return Invocation.invoke (); }
}
}
Implement current Online user restrictions:
Add a judgment at the landing point:
if (new Onlineusermap (). Onlineuser.keyset (). Size () > x
writejson ("The current online user exceeds the limit, please try again later.") ");
}