1
13693261870
2022-09-16 762f2fb45db004618ba099aa3c0bd89dba1eb843
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.landtool.lanbase.common.shiro;
 
import java.awt.Event;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
 
import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListenerAdapter;
import org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
 
import com.landtool.lanbase.common.utils.RedisUtils;
import com.landtool.lanbase.modules.sys.redis.OnlineUserRedis;
 
 
@Component
public class SessionWatcher extends SessionListenerAdapter {
//    @Autowired
//    private OnlineUserRedis onlineUserRedis;
    
 
    
    @Autowired
    private RedisUtils redisUtils;
    
    private static  RedisUtils redisUtil;
    
    @PostConstruct
    public void init() {
    
     log.info("111");
     redisUtil=redisUtils;
 
    }
    private Logger log= LoggerFactory.getLogger(SessionWatcher.class);
    @Override
    public void onStart(Session session){
       session.setTimeout(1000*60*60);
        log.info("一连接");
    }
    
    
    @Override
    public void  onExpiration(Session session){
        
        log.info("失效");
        cleanLogoutUser( session);
    }
    
    
    
    @Override
    public void onStop(Session session){
    
        log.info("停止");
        cleanLogoutUser(session);
    }
    public void cleanLogoutUser(Session session){
        
        if(redisUtil.exists("onlineuser")){
            ConcurrentHashMap infos=redisUtil.get("onlineuser",ConcurrentHashMap.class );
                infos.remove(session.getId().toString());    
                redisUtil.set("onlineuser", infos);
        }
 
    }
    
    
    
 
    
}