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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值