At the same time, only one account can log on to the system.

Source: Internet
Author: User

I used springsecurity to implement this kind of requirement. I didn't consider how to implement it with pure configuration.

Today, my friend asked me not to use springsecurity for implementation. I thought it was feasible. Write it down first and try it at night...

1. Create a map in applicationcontext. The key is the account ID and the value is session.
2. when you log on, you can use the contains () method to determine whether the map contains the ID key. If so, delete the session from the map, put the new session. If not.

3. In the filter, retrieve the session from the map of the application and compare the session ID with your own session. If the values are the same, continue the operation. If the values are different, the user logs in and jumps out of the system and gives a prompt.



In the logon action:
// Judge repeated logins
Servletcontext application = servletactioncontext. getservletcontext ();
Hashmap <string, httpsession> sessionmap = (hashmap <string, httpsession>) application. getattribute ("sessionmap ");
If (null! = Sessionmap ){
Sessionmap. Remove (Enterprise. getuserid ());
Sessionmap. Put (Enterprise. getuserid (), session );
Application. setattribute ("sessionmap", sessionmap );
} Else {
Hashmap <string, httpsession> map = new hashmap <string, httpsession> ();
Map. Put (Enterprise. getuserid (), session );
Application. setattribute ("sessionmap", MAP );


Public class safetyfilter implements filter {
Servletcontext application;
Public void destroy (){


Public void dofilter (servletrequest, servletresponse,
Filterchain chain) throws ioexception, servletexception {
Httpservletrequest request = (httpservletrequest) servletrequest;
Httpservletresponse response = (httpservletresponse) servletresponse;
String servletpath = request. getservletpath (). Trim ();

System. Out. println (servletpath );

// Do not intercept the First Login, password change, and exit the system
If (! "/Transfer/getsignacode.shtml". Equals (servletpath)
&&! "/Safety/updatelogpwd.shtml". Equals (servletpath)
&&! "/System/tologin.shtml". Equals (servletpath)
&&! "/Qyqt/system/tologin.shtml". Equals (servletpath )){

// Determine Session Timeout
If (! Path. Equals ("system ")){
Enterprise en = (enterprise) request. getsession (). getattribute ("enterprise ");
If (en = NULL ){
Response. setcontenttype ("text/html; charset = UTF-8 ");
Printwriter out = response. getwriter ();
Out. Print ("<SCRIPT> parent. Window. Location = '/qyqt/system/tologin.shtml'; </SCRIPT> ");

// Judge repeated logins
Hashmap <string, httpsession> sessionmap = (hashmap <string, httpsession>) application. getattribute ("sessionmap ");
Enterprise user = (enterprise) request. getsession (). getattribute ("enterprise ");
If (null! = Sessionmap & null! = User ){
Httpsession thissession = request. getsession ();
Httpsession othersession = sessionmap. Get (user. getuserid ());
If (! Thissession. GETID (). Equals (othersession. GETID ())){
Response. setcontenttype ("text/html; charset = UTF-8 ");
Printwriter out = response. getwriter ();
Out. Print ("<SCRIPT> parent. Window. Location = '/qyqt/system/tologin.shtml'; </SCRIPT> ");

Chain. dofilter (request, response );

Public void Init (filterconfig arg0) throws servletexception {
Application = arg0.getservletcontext ();


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: 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.