In JSP development, you can log on to a single browser using the Logon account in the spring mvc project. springmvc
In JSP development, you can log on to a single browser using the Logon account in the spring mvc project.
In many web products, you must allow only one account and only one browser to log on at the same time. In other words, when account
In browser 1, log on to account A in browser 2. Account A in browser 1 will be squeezed out. When the user operates the page in browser 1, the page will
Go to the logon page and log on again. So how can we implement this function? The following is a detailed introduction:
Principle
User A uses account a to log on to account a in the browser, and user B then logs on to account A in the browser of another computer. When user B logs on successfully
In the listener class, it is determined that account a has been logged on by user A, and account A of user a is kicked out. At this time, when user A operates the page, the logon page is displayed.
Code Implementation
The LoginListenner listener class, login logon method, and web. xml listener class are used in the implementation process.
LoginListenner
After successful logon, The loginuser account is added to the session to trigger the attributeAdded event in LoginListenner. In this event,
We determine whether there is a session of the currently logged-on account in the map that stores the correspondence between the account and the session. If so, we will remove the session from the map and cancel the session, then, add the newly logged on account and session to map.
The following code is used:
/***** @ ClassName: LoginListenner * @ Description: logon listener class-only accounts are allowed for processing at the same time, single location login * @ author mr_smile2014 605051929@qq.com * @ date November 12, 2014 2:23:41 */public class LoginListenner implements HttpSessionAttributeListener {/*** map used to store the account and session ing */private Map <String, httpSession> map = new HashMap <String, HttpSession> ();/*** triggered when data is put into the session */public void attributeAdded (HttpSessionBindingEve Nt event) {String name = event. getName (); if (name. equals ("loginuser") {User user User = (User) event. getValue (); if (map. get (user. getUserName ())! = Null) {HttpSession session = map. get (user. getUserName (); session. removeAttribute (user. getUserName (); session. invalidate ();} map. put (user. getUserName (), event. getSession () ;}}/*** triggered when data is removed from the session */public void attributeRemoved (HttpSessionBindingEvent event) {String name = event. getName (); if (name. equals ("loginuser") {User user User = (User) event. getValue (); map. remove (user. getUserName () ;}} public void attributeReplaced (HttpSessionBindingEvent event) {} public Map <String, HttpSession> getMap () {return map;} public void setMap (Map <String, httpSession> map) {this. map = map ;}}
Logon Method
Determine and verify the account, password, and verification code. After the verification is passed, put the corresponding user object into the session. The Code is as follows:
/*** Login ** @ param userName * @ param passWord * @ param code * Verification code * @ param type * login type (merchant, operator) * @ param model * @ return */@ RequestMapping ("/login") public String login (String account, String passWord, String code, Model model, HttpServletRequest request) {// login verification and return the Successful Login User Object user User = loginResult (userPhone, passWord, code, request); // put the user object into the session, the attributeAdded event request in LoginListenner is triggered. getSession (). setAttribute ("loginuser", user );}
Web. xml configuration
Configure the LoginListenner listening class to the web. xml file to make the session listening take effect. The configuration is as follows:
<! -- A user can only log on to one host --> <listener-class> com. test. listenner. LoginListenner </listener-class> </listener>
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!