Spring Boot 会话超时

新手上路,请多包涵

server.session-timeout 似乎只适用于嵌入式tomcat。

我放了一条日志语句来检查会话最大间隔时间。手动将war文件部署到tomcat后,我意识到默认会话超时值(30分钟)仍在使用中。

如何使用 spring-boot 设置会话超时值(不是用于嵌入式 tomcat,而是用于独立应用程序服务器)?

原文由 led 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 440
1 个回答

[ 以防万一有人觉得这很有用]

如果您使用 Spring Security,您可以扩展 SimpleUrlAuthenticationSuccessHandler 类并在身份验证成功处理程序中设置 _会话超时_:

 public class NoRedirectSavedRequestAwareAuthenticationSuccessHandler
       extends SimpleUrlAuthenticationSuccessHandler {

    public final Integer SESSION_TIMEOUT_IN_SECONDS = 60 * 30;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication)
                                        throws ServletException, IOException {

        request.getSession().setMaxInactiveInterval(SESSION_TIMEOUT_IN_SECONDS);

        // ...
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginProcessingUrl("/login")
            .successHandler(new NoRedirectSavedRequestAwareAuthenticationSuccessHandler())
            .failureHandler(new SimpleUrlAuthenticationFailureHandler())
            .and().httpBasic();
    }

}

原文由 justin 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进