java 后台和android 交互 保持session通讯

来源:互联网 发布:程序员充电 编辑:程序博客网 时间:2024/06/16 01:28

最近和app 对接登录时,需要做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());



完毕。。。。



原创粉丝点击