Spring Security---ConcurrentSessionFilter

                                 ConcurrentSessionFilter(转)

ConcurrentSessionFilter做的功能比较简单,主要是判断session是否过期以及更新最新访问时间

     通过代码HttpSession session = request.getSession(false);判断获取session

1、首先判断session是否存在--------HttpSession session = request.getSession(false)

    如果session不存在,直接放过,执行下一个过滤器。

   如果存在,则执行第二步

2、根据sessionid从sessionRegistry中获取SessionInformation,从SessionInformation中获取session是否过期

    如果没有过期,则更新SessionInformation的访问日期。

    如果过期,则执行doLogout()方法,这个方法会将session无效,并将SecurityContext中的Authentication中的权限置空,同时在SecurityContenxtHoloder中清除SecurityContext

    然后查看是否有跳转的URL,如果有就跳转,没有就输出提示信息

在ConcurrentSessionFilter有两个重要的类需要知道,一个是sessionRegistry(默认使用sessionRegistryImpl),一个是SessionInformation 。

sessionRegistry顾名思义 session注册表,有维护两个类变量ConcurrentMap<Object,Set<String>> principals 和Map<String, SessionInformation> sessionIds

principals以用户认证信息为key,values值sessionId集合,sessionIds以sessionId为key,以SessionInformation 为values值。

sessionInformation只要存放4个参数,一个是lastRequest;(最近访问时间),一个是sessionId,一个是principal(用户认证权限),一个是expired (是否过期)

这两个类变量的信息会在后面的登陆认证的时候进行赋值

 

代码如下

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession(false);
        if (session != null) {
            SessionInformation info = sessionRegistry.getSessionInformation(session.getId());
            if (info != null) {
                if (info.isExpired()) {
                    // Expired - abort processing
                    doLogout(request, response);
                    String targetUrl = determineExpiredUrl(request, info);
                    if (targetUrl != null) {
                        redirectStrategy.sendRedirect(request, response, targetUrl);
                        return;
                    } else {
                        response.getWriter().print("This session has been expired (possibly due to multiple concurrent " +
                                "logins being attempted as the same user).");
                        response.flushBuffer();
                    }
                    return;
                } else {
                    // Non-expired - update last request date/time
                    sessionRegistry.refreshLastRequest(info.getSessionId());
                }
            }
        }
        chain.doFilter(request, response);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值