C/S模式下session通讯问题,或使用token的原因

临时解决方案:
创建一个session管理工具类(OnlineSession ),用一个POJO类(SessionAndUser )封装session和用户信息,作用:把用户和session进行绑定。

类:OnlineSession

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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import javax.servlet.http.HttpSession;
 
import com.data.entity.table.People;
 
 
public class OnlineSession {
 
    private static List SessionList;     // 在线用户session集合
 
    // 在线用户集合类不能被实例化
    private OnlineSession() {
    }
 
    static {
        if (null == SessionList) {
            // 用于获得指定集合的同步集合(线程安全的)的支持,返回同步集合
            SessionList = Collections.synchronizedList(new ArrayList());
        }
    }
 
    
    public static boolean addSessionAndUser( HttpSession session , People people ){
        if( session == null ){ return false ; }
        System.out.println("调试 : >>>>>>>>      SessionID:"+session.getId()+"获取授权");
        SessionAndUser sau = new SessionAndUser() ;
        sau.setPeople(people);
        sau.setSessionID( session.getId() );
        sau.setSession( session );
        //设置用户最长连接中无动作时长(60min)
        session.setMaxInactiveInterval(60*60);
        return SessionList.add( sau );
    }
    
    
    public static List getList() {
        return SessionList ;
    }
    
    
    public static People getLoginPeople( String userName ){
        for( SessionAndUser temp : SessionList ){
            if( temp.getPeople().getUserName().equals( userName ) ){
                return temp.getPeople() ;
            }
        }
        return null ;
    }
    
    
    public static SessionAndUser getSessionAndUser( String id ) {
        for( SessionAndUser temp : SessionList ){
            if( temp.getSessionID().equals( id ) ){
                return temp ;
            }
        }
        return null ;
    }
    
    
    public static String getSessionID( String userName ){
        for( SessionAndUser temp : SessionList ){
            if( temp.getPeople().getUserName().equals( userName ) ){
                return temp.getSessionID() ;
            }
        }
        return null ;
    }
    
    
    public static boolean  removeSessionAndUser( String userName ){
        for( SessionAndUser temp : SessionList ){
            if( temp.getPeople().getUserName().equals( userName ) ){
                System.out.println("调试 : >>>>>>>>      SessionID:"+temp.getSessionID()+"失效");
                boolean flag = SessionList.remove( temp );
                temp.getSession().invalidate();        //销毁session       
                return flag ;
            }
        }
        return false ;
    }
 
}

类:SessionAndUser

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import javax.servlet.http.HttpSession;
import com.data.entity.table.People;
 
 
public class SessionAndUser {
    private People people; // 登录用户
    private String sessionID; // session ID
    private HttpSession session; // 当前用户session
 
    public People getPeople() {
        return people;
    }
 
    public void setPeople(People people) {
        this.people = people;
    }
 
    public String getSessionID() {
        return sessionID;
    }
 
    public void setSessionID(String sessionID) {
        this.sessionID = sessionID;
    }
原文地址:https://www.cnblogs.com/nimenhao/p/4912963.html