首页>代码>Springboot开发的精简博客系统 >/noteblogv4-master/src/main/java/me/wuwenbin/noteblogv4/config/application/SessionValidator.java
package me.wuwenbin.noteblogv4.config.application;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Enumeration;

/**
 * 定时任务验证用户登录有效期
 * created by Wuwenbin on 2018/2/7 at 22:40
 *
 * @author wuwenbin
 */
@Slf4j
@Component
@EnableScheduling
public class SessionValidator {

    private final NBContext nbContext;

    @Autowired
    public SessionValidator(NBContext nbContext) {
        this.nbContext = nbContext;
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void sessionValidate() {
        log.info("validate session in noteblog application task...");
        Enumeration<String> keys = nbContext.keys();
        while (keys.hasMoreElements()) {
            String uuid = keys.nextElement();
            NBSession session = nbContext.get(uuid);
            if (session != null) {
                if (session.isExpired()) {
                    String info = "delete session for id:[{}], at [{}]";
                    log.info(StrUtil.format(info, session.getId(), LocalDateTime.now()));
                    nbContext.removeSessionUser(uuid);
                }
            }
        }
    }

}
最近下载更多
qiangmin1223  LV12 前天
xiaokang1  LV10 4月23日
zhouenyi  LV6 3月14日
admin_z  LV22 2023年12月21日
WHY-small  LV15 2023年12月20日
fff2003  LV6 2023年12月20日
Rucoding  LV7 2023年10月31日
周敏国  LV9 2023年10月15日
qq573914838  LV5 2023年9月2日
taoshen95  LV14 2023年8月19日
最近浏览更多
qiangmin1223  LV12 前天
whb5566  LV10 前天
zolscy  LV12 4月23日
xiaokang1  LV10 4月23日
xiaozhi丶  LV14 4月22日
qwm374149  LV1 4月19日
akittyboy  LV9 4月16日
李二san  LV1 4月13日
lingtiejing  LV15 4月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友