我已经在spring MVC应用程序中实现了spring会话。它正在我的数据库中创建会话表并存储会话ID。但是我无法更改“ MaxInactiveIntervalInSeconds”的值。在基于XML的配置中,我更改了“ MaxInactiveIntervalInSeconds”值,如下所示。
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds">
<value>60</value>
</property>
</bean>
并且运行正常。但是我无法在基于Java的配置中更改'MaxInactiveIntervalInSeconds'值。我已经尝试了以下方法。
@Bean
public JdbcHttpSessionConfiguration setMaxInactiveIntervalInSeconds(JdbcHttpSessionConfiguration jdbcHttpSessionConfiguration) {
jdbcHttpSessionConfiguration.setMaxInactiveIntervalInSeconds(60);
return jdbcHttpSessionConfiguration;
}
但是它不起作用。
我的SessionConfig和SessionInitializer类如下。
@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;
}
}
和
public class SessionInitializer extends AbstractHttpSessionApplicationInitializer {
}
有什么方法可以实现?
我找到了一种方法。只需添加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);
}
这对我有用。
编辑1找到了另一种方法。这将是正确的方法,
@EnableJdbcHttpSession(maxInactiveIntervalInSeconds = intervalInSeconds)