【问题标题】:How to change MaxInactiveIntervalInSeconds value in spring session java based configuration?如何在基于 Spring 会话 Java 的配置中更改 MaxInactiveIntervalInSeconds 值?
【发布时间】:2020-03-10 10:57:39
【问题描述】:

我已经在 Spring MVC 应用程序中实现了 Spring session。它在我的数据库中创建会话表并存储会话 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 {

}

有什么办法可以做到吗?

【问题讨论】:

    标签: java spring spring-mvc spring-session


    【解决方案1】:

    我找到了办法。只需添加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)
    

    【讨论】:

    • 如何将maxInactiveIntervalInSeconds 设置为属性?我想使用外部属性对其进行配置。类似@EnableJdbcHttpSession(maxInactiveIntervalInSeconds = ${spring.session.timeout})
    • 这个答案可能对你有帮助stackoverflow.com/questions/33586968/…
    猜你喜欢
    • 2014-06-21
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多