【问题标题】:customized session timeout in spring/security春季/安全中的自定义会话超时
【发布时间】:2017-03-05 23:20:22
【问题描述】:

如何在春季自定义会话超时.. 会话超时参数/输入在 web.xml 中设置为 15 分钟。 它工作正常.. 我想在此会话超时发生之前执行几行,并且应该能够决定是否继续进行会话超时.. 这意味着.. 我想检查会话中的一些参数,然后希望有选择地为某些用户进行会话超时(即在 15 分钟不活动后),而其他用户则永远不会发生此超时。

【问题讨论】:

    标签: java spring session spring-security session-timeout


    【解决方案1】:

    使用 HttpSessionListener

    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    public class SessionListener implements HttpSessionListener {
    
        @Override
        public void sessionCreated(HttpSessionEvent event) {
            // ..   event.getSession().getAttribute("xxxx")
            event.getSession().setMaxInactiveInterval(5*60);
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent event) {
            System.out.println("==== Session is destroyed ====");
        }
    }
    

    在 web.xml 中注册

    <web-app ...>
        <listener>
            <listener-class>yourpackage.SessionListener</listener-class>
        </listener>
    </web-app>
    

    或在您的应用程序初始化程序中

    public class AppInitializer implements WebApplicationInitializer {
    
    //...
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            super.onStartup(servletContext);
            servletContext.addListener(new SessionListener());
        }
    //...
    
    }
    

    【讨论】:

    • 谢谢..我需要在会话因会话超时而被破坏之前自定义间隔(setMaxInactiveInterval)..而不是在创建时或销毁之后..有类似的吗?
    • 另外,如果我没有设置 setMaxInactiveInterval(5*60) 那么会遵循 web.xml 中的默认超时时间(即 15 分钟)吗?
    • @adarshabv 然后你可以使用拦截器stackoverflow.com/a/12084748/410677
    猜你喜欢
    • 2012-02-17
    • 2015-10-19
    • 1970-01-01
    • 2015-07-30
    • 2011-06-21
    • 1970-01-01
    • 2016-11-06
    • 2011-12-31
    相关资源
    最近更新 更多