1

I have implemented spring session in spring MVC application. It is creating session tables in my database and storing the session ids. But i am not able to change the 'MaxInactiveIntervalInSeconds' value. In XML based configuration i have changed 'MaxInactiveIntervalInSeconds' value like below.

<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"> 
        <property name="maxInactiveIntervalInSeconds"> 
            <value>60</value> 
        </property> 
     </bean>

and it is working fine. But i am not able to change the 'MaxInactiveIntervalInSeconds' value in java based configuration. I have tried the following.

@Bean
public JdbcHttpSessionConfiguration setMaxInactiveIntervalInSeconds(JdbcHttpSessionConfiguration jdbcHttpSessionConfiguration) {
    jdbcHttpSessionConfiguration.setMaxInactiveIntervalInSeconds(60);
    return jdbcHttpSessionConfiguration;
}

But it is not working.

My SessionConfig and SessionInitializer classes are like below.

@Configuration
@EnableJdbcHttpSession
public class SessionConfig {
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public JdbcHttpSessionConfiguration setMaxInactiveIntervalInSeconds(JdbcHttpSessionConfiguration jdbcHttpSessionConfiguration) {
        jdbcHttpSessionConfiguration.setMaxInactiveIntervalInSeconds(60);
        return jdbcHttpSessionConfiguration;
    }
}

and

public class SessionInitializer extends AbstractHttpSessionApplicationInitializer {

}

Is there any way to achieve this?

Sulthan
  • 354
  • 3
  • 19

1 Answers1

3

I found a way. Just add httpServletRequest.getSession().setMaxInactiveInterval(intervalInSeconds)

    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String login(HttpServletRequest request, HttpServletResponse servletresponse){
       //Your logic to validate the login
       request.getSession().setMaxInactiveInterval(intervalInSeconds);
    }

This worked for me.

EDIT 1 Found another way to do this. This would be the correct way of doing this,

@EnableJdbcHttpSession(maxInactiveIntervalInSeconds = intervalInSeconds)
Sulthan
  • 354
  • 3
  • 19
  • How can I set `maxInactiveIntervalInSeconds` to a property? I want to configure it with an external property. Something like `@EnableJdbcHttpSession(maxInactiveIntervalInSeconds = ${spring.session.timeout})` – Alexander R. Dec 28 '21 at 17:44
  • 1
    This answer might help you https://stackoverflow.com/questions/33586968/how-to-import-value-from-properties-file-and-use-it-in-annotation – Sulthan Dec 30 '21 at 19:34