工作总结——session超时篇二

本文探讨了在Web应用中如何有效判断session是否超时,包括使用HttpSessionBindingListener接口监听session绑定事件的方法及其局限性,以及通过request获取session判断是否为空的可行方案。适用于多用户登录场景。

咳咳咳 言归正传。来讲讲session超时判断的问题。

一开始我的实现方式是实现HttpSessionBindingListener:

public interface HttpSessionBindingListener extends EventListener {



    /**
     *
     * Notifies the object that it is being bound to
     * a session and identifies the session.
     *
     * @param event		the event that identifies the
     *				session 
     *
     * @see #valueUnbound
     *
     */ 

    public void valueBound(HttpSessionBindingEvent event);
    
    

    /**
     *
     * Notifies the object that it is being unbound
     * from a session and identifies the session.
     *
     * @param event		the event that identifies
     *				the session 
     *	
     * @see #valueBound
     *
     */

    public void valueUnbound(HttpSessionBindingEvent event);
    
    
}

实现该接口后,session超时会调用valueUnbound方法。在方法中设置静态常量boolean值通过AUTH注解的AOP判断该常量的值从而判断session是否失效。然后领导来了一句,如果多用户登陆呢???咳咳   懵逼了。所以方法pass。换个方法

  • 用request获取session判断是否为空
       HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        if (request.getSession(false) == null) {
            if (request.getSession(true).isNew()) {

            } else {
                System.out.println("Session 超时");
                throw new Exception("Session time out.");
            }
        }

可行。结束!代码块放在了auth权限校验的注解AOP中。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值