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 8732505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:27:10+00:00 2026-06-13T09:27:10+00:00

I’m working on a grails web application with spring security. I have a requirement

  • 0

I’m working on a grails web application with spring security. I have a requirement for forcing a session expiration after a fixed set of time even if the session is active.

I think I can add filter and check for each request’s last login time:

if (CURRENT_TIME - LAST_LOGIN > ABSOLUTE_SESSIO EXPIRATION) then FORCE LOGOUT

But the problem is that the session is still active on the server until the user makes request.

Is this possible to destroy session immediately after N minutes even if user is using the system?
I was researching tomcat session management and how spring security handles it, but didn’t find any useful information.

Could anybody point me at some example? Has anybody implemented something similar?

  • 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-06-13T09:27:10+00:00Added an answer on June 13, 2026 at 9:27 am

    [Edit: An idea for killing the session at a specific time decided by the login time would be use a task scheduler like Quartz to schedule a task that is passed the session as a parameter. You could then schedule it to call a job that performs a session.invalidate() at a specific point in time. The task would be scheduled at login time.]

    This is how I would do it, but it doesn’t kill the session at the specific time you want. It relies on the user making a request. It’s not fool proof, but you could make the application poll the site every minute or so via an AJAX call perhaps.

    Set a session activation time on the users session. Then add a filter to the web application for all incoming requests that checks the (activation period + the allowed time) exceeds the current time. If it does not, then call session.invalidate();

    i.e. on login

    HttpSession session = request.getSession();
    session.setAttribute( "activation-time", System.currentTimeMillis() );
    

    Then add a filter to web.xml

    <filter>
        <filter-name>SessionFilter</filter-name>
        <filter-class>com.something.SessionFilter</filter-class>
        <init-param>
            <param-name>max-period</param-name>
            <param-value>60000</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SessionFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    The filter would be something along the lines of…

    package com.something;
    
    import java.io.IOException;
    import java.util.Date;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    public class SessionFilter implements Filter {
        private long maxPeriod;
    
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpSession session = request.getSession( false );
            if ( session != null ) {
                long activated = (long) session.getAttribute( "activation-time" );
                if ( System.currentTimeMillis() > ( activated + maxPeriod ) ) {
                     session.invalidate();
                }
            }
            chain.doFilter(req, res);
        }
    
        public void init(FilterConfig config) throws ServletException {
            //Get init parameter
            if ( config.getInitParameter("max-period") == null ) {
                 throw new IllegalStateException( "max-period must be provided" );
            }
            maxPeriod = new Long( config.getInitParameter("max-period") );
        }
    
        public void destroy() {
            //add code to release any resource
        }
    }
    

    If you need to invoke something when the session is invalidated then you can write a HttpSessionListener and configure in web.xml also.

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

Sidebar

Related Questions

I'm building a web application with Grails, using the Acegi/Spring Security plugin. I want
I am working on a web application (using Grails) which will generate a gift
We are working on a new project with Grails 2.0.1 and Spring Security. The
I am writing a web app in Grails with the Acegi/Spring Security plug-in, and
Set up: I have a grails project in IntelliJ Idea. I run the application
I have a Grails 2.0.0 web application that runs in both a DEV and
I am working with a Grails web application and I get so many strange
I am working on a grails web-app The application is running on a very
I am developing a web-application in grails.In that I have implemented video's playing option.For
I am currently working on grails project. I have created eight different plugins. Each

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.