Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:29:31+00:00 2026-05-27T20:29:31+00:00

I am developing a webapp that uses Spring Security as security layer. One important

  • 0

I am developing a webapp that uses Spring Security as security layer.

One important feature for us is to know which user is accessing the application, and how many time they are spending on it.

I’m not sure how to deal with it. Is there some other framework that deals with this kind of usage statistics?

Is there some way to use Spring Security itself to deal with it?

// I’m reading more about Spring Security and it seems that its filters can help me. Any progress will be shared here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:29:31+00:00Added an answer on May 27, 2026 at 8:29 pm

    I think one of the solutions I can think of is using a HttpSessionListener, If you implement a Session listener you could capture the time as and when a new user session is created and destroyed, You could leverage your spring security context holder to get a hold of the uniquename/userid of the logged in user

    I am thinking some thing like this

     public class SesssionListenerImpl implements HttpSessionListener 
     {
      @Override
      public void sessionCreated(HttpSessionEvent httpSessionEvent) 
      {
          String uniqueName =     SecurityContextHolder.getContext().getAuthentication().getName();
         String sessionId = httpSessionEvent.getSession().getId();
         long beginTimeInSeconds = System.currentTimeMillis()/1000;
    //create a record and persist to data base with sessionId, uniquename, sessionBeginTime
    
    }
    
    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) 
    {
    
        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        httpSessionEvent.getSession().getId();
        long endTime = System.currentTimeMillis()/1000;
        //load the record based on sessionId
        //update the record with sessionEndTime
    }
    }
    

    That being said there are few down sides to this approach, If the HTTP Session never gets invalidated then you will end up with some sessions with no end time.

    • If you could gently prod your user base to log out all the time as good practice (although not a practical solution)
    • You could do may be body on load and check if the user is leaving your domain or used the windows close button to close the window and fire a session invalidate to capture the end time

    UPDATE

    You are right I think you could use spring application event mechanism, to do that add this to your web.xml, This listener publishes HTTP session events other wise you won’t be able to access session events even if you implement ApplicationListener

        <listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
      </listener>
    

    Now add a new class that implements ApplicationListener

      @Service
     public class ApplicationSecurityListener implements ApplicationListener<ApplicationEvent>
     {
      @Override
      public void onApplicationEvent(ApplicationEvent event)
      {
    
        if ( event instanceof AuthorizationFailureEvent )
        {
            AuthorizationFailureEvent authorizationFailureEvent = ( AuthorizationFailureEvent ) event;
            System.out.println ( "not authorized:" + authorizationFailureEvent );
        }
        else if ( event instanceof AuthenticationFailureBadCredentialsEvent )
        {
            AuthenticationFailureBadCredentialsEvent badCredentialsEvent = ( AuthenticationFailureBadCredentialsEvent ) event;
            System.out.println ( "badCredentials:" + badCredentialsEvent );
        }
                //login success event
        else if ( event instanceof AuthenticationSuccessEvent )
        {
            AuthenticationSuccessEvent authenticationSuccessEvent = ( AuthenticationSuccessEvent ) event;
            //this will provide user id and password but no session, get source has all the user information in security context
            System.out.println ( "AuthenticationSuccessEvent:" + authenticationSuccessEvent.getSource() );
        }
        //this event will published if you add the HttpSessionEventPublisher to web.xml
        else if ( event instanceof SessionDestroyedEvent )
        {
            SessionDestroyedEvent sessinEvent = ( SessionDestroyedEvent ) event;
            System.out.println ( "SessionDestroyedEvent:" + sessinEvent.getId() );
            //load session if it is not empty
            if(sessinEvent.getSecurityContext() != null)
            {
                System.out.println ( "SessionDestroyedEvent:" + sessinEvent.getSecurityContext().getAuthentication().getName() );
                //update the session with endTime
            }
        }
        else
        {
            //System.out.println ( "undefined: " + event.getClass ().getName () );
        }
    
    
    }
    
    }
    

    There is another event if you would like to capture logout by itself, You can implement LogoutHandler that gives you access to logut event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an intranet webapp in asp.net that has a requirement for storing user
I have a PhoneGap application I am developing for iPad that uses video extensively,
I am developing an application for WebSphere 6.1 which uses a Java servlet. Within
I am developing a webapp that communicates with a backend which performs intensive numerical
I'm developing a Java webapp using Spring, Spring Security, Tomcat and MySQL. Right now
I'm developing web app that user can save his/her work to server. The data
I'm developing a webapp where the user is given the chance to upload his
I am developing a small web app that uses jQuery quite a lot. In
Suppose I'm developing a web app that is heavily dependent on JavaScript and which
A web app I'm developing uses lots of asynchronously loaded images that are often

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.