java web 登录后更新JSESSIONID

    xiaoxiao2021-03-25  13

    来源url:http://blog.csdn.net/u014538198/article/details/41596647

    登录前的请求一般都是http的,http是不安全的,假设用户登录前的JSESSIONID被人取得,如果登录后不变更JSESSIONID的话,即使登录请求是https的,该用户仍然会被他人冒充。

     

    javaweb程序强制更新JSESSIONID的方法

    Java代码   /**   * 重置sessionid,原session中的数据自动转存到新session中   * @param request   */   public static void reGenerateSessionId(HttpServletRequest request){              HttpSession session = request.getSession();              //首先将原session中的数据转移至一临时map中       Map<String,Object> tempMap = new HashMap();       Enumeration<String> sessionNames = session.getAttributeNames();       while(sessionNames.hasMoreElements()){           String sessionName = sessionNames.nextElement();           tempMap.put(sessionName, session.getAttribute(sessionName));       }              //注销原session,为的是重置sessionId       session.invalidate();              //将临时map中的数据转移至新session       session = request.getSession();       for(Map.Entry<String, Object> entry : tempMap.entrySet()){           session.setAttribute(entry.getKey(), entry.getValue());       }   }  

     

     

    PHP重置sessionid的方式参见:http://huangqiqing123.iteye.com/blog/1891051

    转自:http://huangqiqing123.iteye.com/blog/2031455

    顶 0

    转载请注明原文地址: https://ju.6miu.com/read-300295.html

    最新回复(0)