package com.njcn.shiro.listener;

import com.njcn.shiro.session.UserShiroSessionRepository;
import com.njcn.utils.PubUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;

/**
 * 用户会话监听
 *
 * @author hongawen
 * @date 2018/3/21 18:53
 */
public class UserSessionListener implements SessionListener {

    private UserShiroSessionRepository userShiroSessionRepository;

    /**
     * 会话创建完毕后，开始使用过
     *
     * @param session 会话
     */
    @Override
    public void onStart(Session session) {
        System.out.println(session.getClass());
    }

    /**
     * 会话关闭后执行的方法
     *
     * @param session 会话
     */
    @Override
    public void onStop(Session session) {
        //从redis中清除指定session
        userShiroSessionRepository.deleteSession(session.getId());
    }

    /**
     * 会话超时之后执行
     *
     * @param session 会话
     */
    @Override
    public void onExpiration(Session session) {
        //从redis中清除指定session
        userShiroSessionRepository.deleteSession(session.getId());
    }

    public UserShiroSessionRepository getUserShiroSessionRepository() {
        return userShiroSessionRepository;
    }

    public void setUserShiroSessionRepository(UserShiroSessionRepository userShiroSessionRepository) {
        this.userShiroSessionRepository = userShiroSessionRepository;
    }
}
