加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 06:17:42 所属栏目:Java 来源:网络整理
导读:出处: 【前面的话】 在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。? 【官方解释】 ? getSession? public? HttpSession ? getSession (boolean?create) Returns the current? H

出处:

【前面的话】

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

【官方解释】

? getSession?

public?HttpSession?getSession(boolean?create)

Returns the current?HttpSession?associated with this request or,if if there is no current session and?create?is true,returns a new session.

If?create?is?false?and the request has no valid?HttpSession,this method returns?null.

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;?false?to return?null?if there's no current session

Returns:?the?HttpSession?associated with this request or?null?if?create?is?false?and the request has no valid session

译:

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

简而言之:

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

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

【问题和bug】:

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

[java]?
?
    HttpSession?session?=?request.getSession();?????
  1. String?user_name?=?session.getAttribute();??

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

<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools">
[java]?<a class="ViewSource" title="view plain" href="http://blog.csdn.net/xxd851116/article/details/4296866"&gt;view plain<a class="CopyToClipboard" title="copy" href="http://blog.csdn.net/xxd851116/article/details/4296866"&gt;copy

?
    HttpSession?session?=?request.getSession();??
  1. ?(session?!=?)?{??
  2. ????String?user_name?=?session.getAttribute();??
  3. }??

【投机取巧】:

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

<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools">
[java]?<a class="ViewSource" title="view plain" href="http://blog.csdn.net/xxd851116/article/details/4296866"&gt;view plain<a class="CopyToClipboard" title="copy" href="http://blog.csdn.net/xxd851116/article/details/4296866"&gt;copy

?
    ?
  1. ?
  2. ?
  3. ?
  4. ?
  5. ?
  6. null?if?not?found?
  7. ??
  8. ??Object?getSessionAttribute(HttpServletRequest?request,?String?name)?{??
  9. ????Assert.notNull(request,?);??
  10. ????HttpSession?session?=?request.getSession();??
  11. ?????(session?!=????session.getAttribute(name)?:?);??
  12. }??

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

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

<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools">
[java]?<a class="ViewSource" title="view plain" href="http://blog.csdn.net/xxd851116/article/details/4296866"&gt;view plain<a class="CopyToClipboard" title="copy" href="http://blog.csdn.net/xxd851116/article/details/4296866"&gt;copy

?
    HttpSession?session?=?request.getSession();??
  1. String?user_name?=?WebUtils.getSessionAttribute(reqeust,?); ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读