3
13693261870
2022-09-16 63ba114e70e380442fcbed4a5157ee52c9491216
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.landtool.lanbase.modules.sys.redis;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
 
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.landtool.lanbase.common.shiro.ShiroToken;
import com.landtool.lanbase.common.utils.RedisUtils;
import com.landtool.lanbase.common.utils.ShiroUtils;
import com.landtool.lanbase.modules.log.entity.LogAction;
import com.landtool.lanbase.modules.log.service.LogActionService;
/**
 * redis人员在线
 * @author zsx
 *
 */
@Component
public class OnlineUserRedis {
    
    private static final String NAME = "onlineuser";
 
    @Autowired
    private RedisUtils redisUtils;
    
 
 
    
    @Autowired
    private LogActionService logservice;
    
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 
//    /**
//     * 从session中获取记录的验证码
//     */
//    private Session getSessionByUsername(String username) {
//        DefaultWebSecurityManager securityManager=(DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
//        DefaultWebSessionManager sessionManager=(DefaultWebSessionManager) securityManager.getSessionManager();
//        Collection<Session> sessions=sessionManager.getSessionDAO().getActiveSessions();
//        Object attribute;
//        String user;
//        Session ses = null;
//         for(Session session:sessions){
//             attribute=session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
//             user=((SimplePrincipalCollection) attribute).toString();
//             if(user.equals(username)){
//                 ses=session;
//                 session.stop();
//                 break;
//                
//             }
//         }
//         return ses;
//    }
//    
    public void removeDownlineUser(){
 
        
        if(redisUtils.exists(NAME)){
            ConcurrentHashMap infos=redisUtils.get(NAME,ConcurrentHashMap.class );    
             Iterator it=  infos.keySet().iterator();
             while(it.hasNext()){
                Object key=     it.next();
                String sessioninfo=   (String) infos.get(key);
                JSONObject json=JSON.parseObject(sessioninfo);
                Double temp=json.getDouble("time");
                //这里与用户最后一次操作时间进行判断
                Map<String,Object>  map=new HashMap<>();
                map.put("UserName", json.getString("username"));
                map.put("appid", 1);
                map.put("lowerOffset", 0);
                map.put("limit", 1);
             List<LogAction>  list= logservice.queryList(map);
             if(list!=null&!list.isEmpty()){
                 if((System.currentTimeMillis()-list.get(0).getDodate().getTime())>10*1000 && 
                         (System.currentTimeMillis()-temp)>10*1000 ){
                     infos.remove(key);
                     
                 }
             }
             }
                redisUtils.set(NAME, infos);
        }
    }
    
    public void saveOnlineUser(String username,Session session){
        if(redisUtils.exists("onlineuser")){
            ConcurrentHashMap<String,Object> infos=redisUtils.get("onlineuser",ConcurrentHashMap.class );
            JSONObject json=new JSONObject();
            List<String> needtoremove=new ArrayList<String>();
             json.put("time", new Long(System.currentTimeMillis()));
             json.put("username",username);
                
                infos.forEach(new BiConsumer<String, Object>() {
 
                    @Override
                    public void accept(String t, Object u) {
                        JSONObject json=JSONObject.parseObject(u.toString());
                        if(json.getString("username").equals(username)){
                          needtoremove.add(t);
                        }
                        
                    }
                });
                if(!needtoremove.isEmpty()){
                    needtoremove.forEach(new Consumer<String>() {
 
                        @Override
                        public void accept(String t) {
                            infos.remove(t);
                            
                        }
                    });
                
                }
                infos.put(session.getId().toString(), json.toJSONString());    
                redisUtils.set("onlineuser", infos);
        }else{
        ConcurrentHashMap<String,Object> onlineuser=new ConcurrentHashMap<String,Object>();    
        JSONObject json=new JSONObject();
         json.put("time", new Long(System.currentTimeMillis()));
         json.put("username",username);
         onlineuser.put(session.getId().toString(), json.toJSONString());    
            redisUtils.set("onlineuser", onlineuser);
        }
 
    }
    
    public void cleanLogoutUser(Session session){
        if(redisUtils.exists("onlineuser")){
            ConcurrentHashMap infos=redisUtils.get("onlineuser",ConcurrentHashMap.class );
                infos.remove(session.getId().toString());    
                redisUtils.set("onlineuser", infos);
        }
 
    }
    
    
}