失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

时间:2023-03-24 04:44:15

相关推荐

对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

本文属于本人原创,转载请注明出处:/xxd851116/archive//06/25/4296866.aspx

【前面的话】

在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。

【官方解释】

getSession

publicHttpSessiongetSession(booleancreate)

Returns the currentHttpSessionassociated with this request or, if if there is no current session andcreateis true, returns a new session.

Ifcreateisfalseand the request has no validHttpSession, this method returnsnull.

To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.

Parameters:true- to create a new session for this request if necessary;falseto returnnullif there's no current session

Returns:theHttpSessionassociated with this request ornullifcreateisfalseand the request has no valid session

译:

getSession(booleancreate)意思是返回当前reqeust中的HttpSession,如果当前reqeust中的HttpSession为null,当create为true,就创建一个新的Session,否则返回null;

简而言之:

HttpServletRequest.getSession(ture) 等同于HttpServletRequest.getSession()

HttpServletRequest.getSession(false) 等同于 如果当前Session没有就为null;

【问题和bug】:

我周围很多同事是这样写的;

[java]view plaincopy print? HttpSessionsession=request.getSession();//anewsessioncreatedifnosessionexists,哈哈!完蛋啦!如果session不存在的话你又创建了一个!Stringuser_name=session.getAttribute("user_name");

需要注意的地方是request.getSession() 等同于 request.getSession(true),除非我们确认session一定存在或者sesson不存在时明确有创建session的需要,否则尽量使用request.getSession(false)。在使用request.getSession()函数,通常在action中检查是否有某个变量/标记存放在session中。这个场景中可能出现没有session存在的情况,正常的判断应该是这样:

[java]view plaincopy print? HttpSessionsession=request.getSession(false);if(session!=null){Stringuser_name=session.getAttribute("user_name");}

【投机取巧】:

如果项目中用到了Spring(其实只要是Java的稍大的项目,Spring是一个很好的选择),对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequest request, String name)方法,看看高手写的源码吧:哈哈。。

[java]view plaincopy print? /***Checkthegivenrequestforasessionattributeofthegivenname.*Returnsnullifthereisnosessionorifthesessionhasnosuchattribute.*Doesnotcreateanewsessionifnonehasexistedbefore!*@paramrequestcurrentHTTPrequest*@paramnamethenameofthesessionattribute*@returnthevalueofthesessionattribute,or<code>null</code>ifnotfound*/publicstaticObjectgetSessionAttribute(HttpServletRequestrequest,Stringname){Assert.notNull(request,"Requestmustnotbenull");HttpSessionsession=request.getSession(false);return(session!=null?session.getAttribute(name):null);}

注:Assert是Spring工具包中的一个工具,用来判断一些验证操作,本例中用来判断reqeust是否为空,若为空就抛异常。

上面的代码又可以简洁一下啦,看吧:

[java]view plaincopy print? HttpSessionsession=request.getSession(false);Stringuser_name=WebUtils.getSessionAttribute(reqeust,"user_name");

如果觉得《对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。