Session User account authentication (one user login, kick out the previous user)

Source: Internet
Author: User

Configure in Web. xml:

< Listener >    < Listener-class >cn.edu.hbcf.common.listener.SessionAttributeListener</listener-class>   </listener>

Then write an entity class:

 PackageCn.edu.hbcf.common.listener;ImportJava.util.Map;ImportJava.util.concurrent.ConcurrentHashMap;Importjavax.servlet.http.HttpSession;ImportJavax.servlet.http.HttpSessionAttributeListener;Importjavax.servlet.http.HttpSessionBindingEvent;Importcn.edu.hbcf.common.constants.WebConstants;Importcn.edu.hbcf.privilege.pojo.BaseUsers; Public classSessionattributelistenerImplementsHttpsessionattributelistener { Public StaticMap<string, httpsession> sessionmap=NewConcurrenthashmap<string, httpsession>(); @Override Public voidattributeadded (httpsessionbindingevent arg0) {if(Arg0.getname (). Equals (Webconstants.current_user)) {//Enumeration session = Arg0.getsession (). Getservletcontext (). Getattributenames ();HttpSession session =arg0.getsession (); //ServletContext application = Session.getservletcontext ();Baseusers user =(baseusers) Session.getattribute (Webconstants.current_user); if(Sessionmap.containskey (User.getaccount ())) {//kick off the previous login                 Try{HttpSession Session2=Sessionmap.remove (User.getaccount ());                Session2.invalidate (); } Catch(Exception e) {}//Application.removeattribute (User.getaccount ());} sessionmap.put (User.getaccount (), session); //Application.setattribute (User.getaccount (), user);}} @Override Public voidattributeremoved (Httpsessionbindingevent arg0) {} @Override Public voidattributereplaced (httpsessionbindingevent se) {//TODO auto-generated Method Stub            }}

Session User account authentication (one user login, kick out the previous user)

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.