Sulthan
Sulthan

Reputation: 374

How to change MaxInactiveIntervalInSeconds value in spring session java based configuration?

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?

Upvotes: 1

Views: 2320

Answers (1)

Sulthan
Sulthan

Reputation: 374

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)

Upvotes: 3

Related Questions