Javaweb implementation of online user restrictions and multiple logon restrictions for the same account

Source: Internet
Author: User
Tags stub

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.") ");
}

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.