Spring Security Maximum session timeout not working

We have this



    .logoutRequestMatcher(new AntPathRequestMatcher("/logout-gimli-user")).permitAll()
    .deleteCookies("JSESSIONID", "sessionid" /*, "sessdetail", "countfr"*/ );


    .invalidSessionUrl("/login?invalidSession") //dokunma
    .maximumSessions(1) //dokunma
    .maxSessionsPreventsLogin(true) //dokunma


public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

And in application.properties, we have


When a user tries to login, user puts password + username, then one time code is sent to user. After this, user needs to put this code and then can view pages.

But if user does not put that code but only puts usernamepassword, and closes browser, logout is not working. Because logout is not invoked.

But timeout should work and kill after 3 minutes. Or tomcat should kill the session (because we deploy to external tomcat 9).

I tried this https://stackoverflow.com/a/41450580/11369236

I added

public static ServletListenerRegistrationBean httpSessionEventPublisher() {
    return new ServletListenerRegistrationBean(new HttpSessionEventPublisher());

but still same.

I put

List<SessionInformation> sessions = sessionRegistry.getAllSessions(authentication.getPrincipal(), false);


public Authentication authenticate(Authentication authentication) {

in public class CustomAuthenticationProviderWithRoles implements AuthenticationProvider {

and tried lots of logins without one time code confirm and saw that, sessions are increasing.

But when i try with putting one time code, it does not allow because of maxSessionsPreventsLogin

like here:


Login page code:

 <form method="POST" th:action="@{/login}">
<input autocomplete="off" class="form-control" id="mobile" name="username"
   <input autocomplete="off" class="form-control password" name="password"

                            <button class="btn btn btn-block btn-primary btn-lg"
                                            value="Log In">LOGIN

this is for login:


and successhandler does this for successfull login:


Then, it sets the seconds which to count from for putting code. And sends another view to put code and which contains another form and submit button.

What can be best practice? For example user can close the page after putting user name password but session still remains. Despite there are timeouts.

I can use this and it solves it but I already session timeout in application.properties:

 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response

This Post Has One Comment

  1. No Fault

    Spring should probably allow an auto-configuration for your case above for the reactive stack as it does for servlet.

    However, “session” is state and that state won’t scale unless there is some persistent storage backing it. You can use the Spring Session abstraction with an in-memory ReactiveSessionRepository even if you don’t (yet) have a backing store like Redis or something. When you do get a proper supported backing store and add the corresponding dependencies, you can delete your in-memory ReactiveSessionRepository as spring boot will auto-configure your ReactiveSessionRepository for you.

    First, add the spring session dependency


    Second, manually create your ReactiveSessionRepository bean. (Note: this can be auto-configured for you if you’re using Redis instead of in-memory, etc.)

    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.autoconfigure.session.SessionProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.session.ReactiveMapSessionRepository;
    import org.springframework.session.ReactiveSessionRepository;
    import org.springframework.session.config.annotation.web.server.EnableSpringWebSession;

    import java.util.concurrent.ConcurrentHashMap;

    * This ReactiveSessionRepository isn’t auto-configured so we need to create it and manually set the timeout on it.
    * Later, ReactiveRedisSessionRepository will be auto-configured so we can delete this
    // https://www.baeldung.com/spring-session-reactive#in-memory-configuration
    @RequiredArgsConstructor // if lombok
    @Slf4j // if lombok
    public class SessionConfig {

    private final SessionProperties sessionProperties;

    public ReactiveSessionRepository reactiveSessionRepository() {
    ReactiveMapSessionRepository sessionRepository = new ReactiveMapSessionRepository(new ConcurrentHashMap());
    int defaultMaxInactiveInterval = (int) sessionProperties.getTimeout().toSeconds();
    log.info(“Set in-memory session defaultMaxInactiveInterval to {} seconds.”, defaultMaxInactiveInterval);
    return sessionRepository;

Leave a Reply