1. 程式人生 > >java 後臺和android 互動 保持session通訊

java 後臺和android 互動 保持session通訊

主要實現是:使用者登入成功後返回sessionID 給app ,app 上需要使用者登入後才能操作的,在每次請求的時候把sessionID 當成引數傳過來。

web.xml程式碼:(主要是建立一個監聽)

<listener>
<listener-class>com.ptpl.controller.ui.MySessionListener</listener-class>
</listener>

MySessionListener程式碼:

package com.ptpl.controller.ui;


import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


public class MySessionListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
    MySessionContext.AddSession(httpSessionEvent.getSession());
    }


    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        MySessionContext.DelSession(session);
    }
}

MySessionContext程式碼:

package com.ptpl.controller.ui;


import java.util.HashMap;


import javax.servlet.http.HttpSession;


public class MySessionContext {


private static HashMap mymap = new HashMap();


    public static synchronized void AddSession(HttpSession session) {
        if (session != null) {
            mymap.put(session.getId(), session);
        }
    }


    public static synchronized void DelSession(HttpSession session) {
        if (session != null) {
            mymap.remove(session.getId());
        }
    }


    public static synchronized HttpSession getSession(String session_id) {
        if (session_id == null)
        return null;
        return (HttpSession) mymap.get(session_id);
    }
}

我寫了個攔截器進行使用者需要登入後才能進行操作,所以每次請求的時候會根據sessionID 找到session,判斷當前使用者資訊是否存在session 裡面;

這裡是攔截器的程式碼:

package com.ptpl.core.interceptor;


import java.util.HashMap;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


import com.alibaba.fastjson.JSON;
import com.ptpl.controller.ui.AppSession_Constant;
import com.ptpl.controller.ui.MySessionContext;
import com.ptpl.model.UserAccountSafeInfo;
import com.ptpl.model.UserBaseAccountInfo;
import com.ptpl.web.util.StringUtil;


public class UserAppInterceptor  implements HandlerInterceptor{


@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
  
}


@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
  
}


@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
  String sessionId = request.getParameter("sessionId");
if(StringUtil.isEmpty(sessionId)){
Map<String,String> hashMap = new HashMap<>();
hashMap.put(AppSession_Constant.RESULT, AppSession_Constant.PARAMSERROR);
hashMap.put(AppSession_Constant.RESULTCODE, "sessionId_null");
hashMap.put(AppSession_Constant.MESSAGE, "提示:sessionId 找不到!");
String str = JSON.toJSONString(hashMap);
StringUtil.sendJsonData(response, str);
return false;
}

HttpSession session = MySessionContext.getSession(sessionId);
if(session == null){
Map<String,String> hashMap = new HashMap<>();
hashMap.put(AppSession_Constant.RESULT, AppSession_Constant.LOGOUT);
hashMap.put(AppSession_Constant.RESULTCODE, "logout");
hashMap.put(AppSession_Constant.MESSAGE, "提示:因您操作超時!請重新登入!");
String str = JSON.toJSONString(hashMap);
StringUtil.sendJsonData(response, str);
return false;
}

UserBaseAccountInfo  userBaseAccountInfo = (UserBaseAccountInfo) session.getAttribute(AppSession_Constant.APPUSER);
UserAccountSafeInfo  userAccountSafeInfo = (UserAccountSafeInfo) session.getAttribute(AppSession_Constant.APPUSERACCOUNTSAFEINFO);
if(userBaseAccountInfo != null && userAccountSafeInfo != null){
return true;
}else{
Map<String,String> hashMap = new HashMap<>();
hashMap.put(AppSession_Constant.RESULT, AppSession_Constant.LOGOUT);
hashMap.put(AppSession_Constant.RESULTCODE, "logout");
hashMap.put(AppSession_Constant.MESSAGE, "提示:因您操作超時!請重新登入!");
String str = JSON.toJSONString(hashMap);
StringUtil.sendJsonData(response, str);
return false;
}
}


}

我們只需要在使用者登入成功的時候把使用者放進session,並把session放進去就可以了

這裡是登入程式碼:

 其他的邏輯程式碼自己看這辦。

       這樣我們只需要把sessionID 的值返回給app,然後app 每次請求的時候發sessionID 傳送過來就可以保持session通訊了。

String sessionId = request.getSession().getId();//sessionID ,這裡我就沒有用加密了,需要加密的自行加密
MySessionContext.AddSession(request.getSession());

完畢。。。。
--------------------- 
作者:phpfzh 
來源:CSDN 
原文:https://blog.csdn.net/phpfzh/article/details/72518850 
版權宣告:本文為博主原創文章,轉載請附上博文連結!