Cookie跨域setDomain和setPath

<h1>
    <span class="link_title"><a href="/zhyh1435589631/article/details/51999915">
    Cookie跨域setDomain和setPath            
    </a></span>
</h1>
    <div class="article_manage clearfix">
    <div class="article_l">
        <span class="link_categories">
        标签:
          <a href="http://www.csdn.net/tag/cookie" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">cookie</a><a href="http://www.csdn.net/tag/spring" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">spring</a><a href="http://www.csdn.net/tag/java" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">java</a>
        </span>
    </div>
    <div class="article_r">
        <span class="link_postdate">2016-07-22 22:38</span>
        <span class="link_view" title="阅读次数">794人阅读</span>
        <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
        <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('Cookie%e8%b7%a8%e5%9f%9fsetDomain%e5%92%8csetPath','51999915');return false;" title="收藏" target="_blank">收藏</a></span>
         <span class="link_report"> <a href="#report" onclick="javascript:report(51999915,2);return false;" title="举报">举报</a></span>

    </div>
</div>
<div class="embody" style="display:none" id="embody">
    <span class="embody_t">本文章已收录于:</span>
    <div class="embody_c" id="lib" value="{&quot;err&quot;:0,&quot;msg&quot;:&quot;ok&quot;,&quot;data&quot;:[]}"></div>
</div>
<style type="text/css">        
        .embody{
            padding:10px 10px 10px;
            margin:0 -20px;
            border-bottom:solid 1px #ededed;                
        }
        .embody_b{
            margin:0 ;
            padding:10px 0;
        }
        .embody .embody_t,.embody .embody_c{
            display: inline-block;
            margin-right:10px;
        }
        .embody_t{
            font-size: 12px;
            color:#999;
        }
        .embody_c{
            font-size: 12px;
        }
        .embody_c img,.embody_c em{
            display: inline-block;
            vertical-align: middle;               
        }
         .embody_c img{               
            width:30px;
            height:30px;
        }
        .embody_c em{
            margin: 0 20px 0 10px;
            color:#333;
            font-style: normal;
        }
</style>
<script type="text/javascript">
    $(function () {
        try
        {
            var lib = eval("("+$("#lib").attr("value")+")");
            var html = "";
            if (lib.err == 0) {
                $.each(lib.data, function (i) {
                    var obj = lib.data[i];
                    //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";
                    html += ' <a href="' + obj.url + '" target="_blank">';
                    html += ' <img src="' + obj.logo + '">';
                    html += ' <em><b>' + obj.name + '</b></em>';
                    html += ' </a>';
                });
                if (html != "") {
                    setTimeout(function () {
                        $("#lib").html(html);                      
                        $("#embody").show();
                    }, 100);
                }
            }      
        } catch (err)
        { }
        
    });
</script>
  <div class="category clearfix">
    <div class="category_l">
       <img src="http://static.blog.csdn.net/images/category_icon.jpg">
        <span>分类:</span>
    </div>
    <div class="category_r">
                <label onclick="GetCategoryArticles('6229381','zhyh1435589631','top','51999915');">
                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">java<em>(8)</em></span>
                  <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                  <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                    <div class="subItem">
                        <div class="subItem_t"><a href="http://blog.csdn.net/zhyh1435589631/article/category/6229381" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                        <ul class="subItem_l" id="top_6229381">                            
                        </ul>
                    </div>
                </label>                    
                <label onclick="GetCategoryArticles('6303274','zhyh1435589631','top','51999915');">
                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">spring<em>(3)</em></span>
                  <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                  <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                    <div class="subItem">
                        <div class="subItem_t"><a href="http://blog.csdn.net/zhyh1435589631/article/category/6303274" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                        <ul class="subItem_l" id="top_6303274">                            
                        </ul>
                    </div>
                </label>                    
    </div>
</div>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/category.js"></script>  


点我进入原文

今天遇到了Cookie的问题,网上搜来些资料,记录一下备忘!
正常的cookie只能在一个应用中共享,即一个cookie只能由创建它的应用获得。 
1.可在同一应用服务器内共享方法:设置cookie.setPath("/"); 
本机tomcat/webapp下面有两个应用:cas和webapp_b, 
1)原来在cas下面设置的cookie,在webapp_b下面获取不到,path默认是产生cookie的应用的路径。
2)若在cas下面设置cookie的时候,增加一条cookie.setPath("/");或者cookie.setPath("/webapp_b/");就可以在webapp_b下面获取到cas设置的cookie了。 
3)此处的参数,是相对于应用服务器存放应用的文件夹的根目录而言的(比如tomcat下面的webapp),因此cookie.setPath("/");之后,可以在webapp文件夹下的所有应用共享cookie,而cookie.setPath("/webapp_b/");是指cas应用设置的cookie只能在webapp_b应用下的获得,即便是产生这个cookie的cas应用也不可以。 
4)设置cookie.setPath("/webapp_b/jsp")或者cookie.setPath("/webapp_b/jsp/")的时候,只有在webapp_b/jsp下面可以获得cookie,在webapp_b下面但是在jsp文件夹外的都不能获得cookie。 
5)设置cookie.setPath("/webapp_b");,是指在webapp_b下面才可以使用cookie,这样就不可以在产生cookie的应用cas下面获取cookie了 
6)有多条cookie.setPath("XXX");语句的时候,起作用的以最后一条为准。 
6)设置多个path的方法??? 
2.跨域共享cookie的方法:设置cookie.setDomain(".jszx.com"); 
A机所在的域:home.langchao.com,A有应用cas 
B机所在的域:jszx.com,B有应用webapp_b 
1)在cas下面设置cookie的时候,增加cookie.setDomain(".jszx.com");,这样在webapp_b下面就可以取到cookie。 
2)这个参数必须以“.”开始。 
3)输入url访问webapp_b的时候,必须输入域名才能解析。比如说在A机器输入:http://lc-bsp.jszx.com:8080/webapp_b,可以获取cas在客户端设置的cookie,而B机器访问本机的应用,输入:http://localhost:8080/webapp_b则不可以获得cookie。 
4)设置了cookie.setDomain(".jszx.com");,还可以在默认的home.langchao.com下面共享。 
5)设置多个域的方法??? 
最近的工作经常用到cookie,看了一些资料,虽然原来依赖cookie比较多的方案已经改成现在这个了,但是当时看得一些东西还是最好

记下来,免得过些天就一干二净了,怪可惜的。老梁说过这样的文字最好不要是纯文本的,说得是。本来就已经很抽象了,最好弄点图表,就算用123列出来也好。


    <div id="digg" articleid="51999915">
        <dl id="btnDigg" class="digg digg_disable" onclick="btndigga();">
           
             <dt>顶</dt>
            <dd>0</dd>
        </dl>
       
          
        <dl id="btnBury" class="digg digg_disable" onclick="btnburya();">
          
              <dt>踩</dt>
            <dd>0</dd>               
        </dl>
        
    </div>
 <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank">&nbsp;</a>   </div>
<div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank">&nbsp;</a></div>
<script type="text/javascript">
            function btndigga() {
                $(".tracking-ad[data-mod='popu_222'] a").click();
            }
            function btnburya() {
                $(".tracking-ad[data-mod='popu_223'] a").click();
            }
        </script>
<div style="clear:both; height:10px;"></div>


    <div class="similar_article" style="">
            <h4>我的同类文章</h4>
            <div class="similar_c" style="margin:20px 0px 0px 0px">
                <div class="similar_c_t">
                            <label class="similar_cur">
                                <span style="cursor:pointer" onclick="GetCategoryArticles('6229381','zhyh1435589631','foot','51999915');">java<em>(8)</em></span>
                            </label>
                            <label class="">
                                <span style="cursor:pointer" onclick="GetCategoryArticles('6303274','zhyh1435589631','foot','51999915');">spring<em>(3)</em></span>
                            </label>
                </div>
               
                <div class="similar_wrap tracking-ad" data-mod="popu_141" style="max-height:195px;">
                    <a href="http://blog.csdn.net" style="display:none" target="_blank">http://blog.csdn.net</a>
                    <ul class="similar_list fl"><li><em>•</em><a href="http://blog.csdn.net/zhyh1435589631/article/details/52074138" id="foot_aritcle_52074138undefined3229522214588181" target="_blank" title="华为2017上合地区码上来试试模拟试题">华为2017上合地区码上来试试模拟试题</a><span>2016-07-30</span><label><i>阅读</i><b>735</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zhyh1435589631/article/details/51986645" id="foot_aritcle_51986645undefined4588578580917666" target="_blank" title="SpringMVC 拦截器实现原理跟登录实现">SpringMVC 拦截器实现原理跟登录实现</a><span>2016-07-21</span><label><i>阅读</i><b>247</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zhyh1435589631/article/details/51891670" id="foot_aritcle_51891670undefined8941275688874735" target="_blank" title="IntelliJ IDEA 2016 破解旗舰版">IntelliJ IDEA 2016 破解旗舰版</a><span>2016-07-12</span><label><i>阅读</i><b>8512</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zhyh1435589631/article/details/51593185" id="foot_aritcle_51593185undefined9891112133760971" target="_blank" title="线程池的介绍及简单实现">线程池的介绍及简单实现</a><span>2016-06-06</span><label><i>阅读</i><b>189</b></label></li> </ul>

                    <ul class="similar_list fr"><li><em>•</em><a href="http://blog.csdn.net/zhyh1435589631/article/details/52006658" id="foot_aritcle_52006658undefined49484667739528776" target="_blank" title="正确理解ThreadLocal">正确理解ThreadLocal</a><span>2016-07-23</span><label><i>阅读</i><b>188</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zhyh1435589631/article/details/51967307" id="foot_aritcle_51967307undefined6763230205251469" target="_blank" title="Java注解Annotation详解">Java注解Annotation详解</a><span>2016-07-20</span><label><i>阅读</i><b>187</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zhyh1435589631/article/details/51864170" id="foot_aritcle_51864170undefined16065203240983816" target="_blank" title="Spring 入门">Spring 入门</a><span>2016-07-08</span><label><i>阅读</i><b>213</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/zhyh1435589631/article/details/51412218" id="foot_aritcle_51412218undefined3783189567228753" target="_blank" title="Java 核心技术 I chap 1 ~ 4 阅读摘要">Java 核心技术 I chap 1 ~ 4 阅读摘要</a><span>2016-05-14</span><label><i>阅读</i><b>166</b></label></li> </ul>
                </div>
            </div>
        </div>    
<script type="text/javascript">
    $(function () {
        GetCategoryArticles('6229381', 'zhyh1435589631','foot','51999915');
    });
</script>
 <div>
         <div class="J_adv" data-view="true" data-mod="ad_popu_206" data-mtp="43" data-order="114" data-con="ad_content_1901" style="width: 960px; height: 90px;"><script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:inline-block;width:960px;height:90px" data-ad-client="ca-pub-8990951720398508" data-ad-slot="8267689356/3776917242" data-adsbygoogle-status="done"><ins id="aswift_0_expand" style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:960px;background-color:transparent"><ins id="aswift_0_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:960px;background-color:transparent"><iframe width="960" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;"></iframe></ins></ins></ins><script>(adsbygoogle=window.adsbygoogle || []).push({});</script></div> 
</div>
 <dt><span>猜你在找</span></dt>    





<div id="adCollege" style="width: 42%;float: left;"> 
    <script src="http://csdnimg.cn/jobreco/job_reco.js" type="text/javascript"></script> 
    <script type="text/javascript">
        csdn.position.showEdu({
            sourceType: "blog",
            searchType: "detail",
            searchKey: "51999915",
            username: "",
            recordcount: "5",
            containerId: "adCollege" //容器DIV的id。 
        });
    </script> 
<div class="tracking-ad" data-mod="popu_84"><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/3117" title="jsp基础入门由浅入深(要javase基础,servlet,javaee必经之路)" strategy="v4:content" target="_blank">jsp基础入门由浅入深(要javase基础,servlet,javaee必经之路)</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/2457" title="JavaWeb实训课-7天掌握JSP/Servlet-顾老师就业课系列" strategy="v4:content" target="_blank">JavaWeb实训课-7天掌握JSP/Servlet-顾老师就业课系列</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/2704" title="JSP从入门到精通+点餐系统实战" strategy="v4:content" target="_blank">JSP从入门到精通+点餐系统实战</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/662" title="Java Swing、JDBC开发桌面级应用" strategy="v4:content" target="_blank">Java Swing、JDBC开发桌面级应用</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/3114" title="Servlet入门到精通(备java基础,jsp、javaee、java框架必备)" strategy="v4:content" target="_blank">Servlet入门到精通(备java基础,jsp、javaee、java框架必备)</a></dd></div></div>  


 <div id="res" data-mod="popu_36" class="tracking-ad" style="width: 42%; float: left; margin-right: 30px; display: block;"><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/alanzyy/article/details/51298889" title="Windows10 SpringMVC中需要使用setPath才能保证cookie保存成功" strategy="SearchAlgorithm" target="_blank">Windows10 SpringMVC中需要使用setPath才能保证cookie保存成功</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/hnd978142833/article/details/52673096" title="Cookie的setPathString方法" strategy="SearchAlgorithm" target="_blank">Cookie的setPathString方法</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/ahhsxy/article/details/6716136" title="删除cookie的时候setMaxAge0有可能不起作用需要加上cookiesetPath" "domain也很重要"="" strategy="SearchAlgorithm" target="_blank">删除cookie的时候setMaxAge0有可能不起作用需要加上cookiesetPath""domain也很重要</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/hnxiangjiansheng/article/details/43537305" title="CP" cao="" psa="" our"="" 用p3p="" header解决iframe跨域访问cookie"="" strategy="SearchAlgorithm" target="_blank">CP"CAO PSA OUR" 用P3P header解决iframe跨域访问cookie</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/zengyi083011/article/details/44079051" title="跨域访问cookie之CORS的完美解决方案" strategy="SearchAlgorithm" target="_blank">跨域访问cookie之CORS的完美解决方案</a></dd></div>
<div id="ad_cen">        
                  <div class="J_adv" data-view="true" data-mod="ad_popu_199" data-mtp="43" data-order="424" data-con="ad_content_2023" style="width: 960px; height: 90px;"><script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:inline-block;width:960px;height:90px" data-ad-client="ca-pub-7443704194229694" data-ad-slot="3705859384" data-adsbygoogle-status="done"><ins id="aswift_1_expand" style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:960px;background-color:transparent"><ins id="aswift_1_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:960px;background-color:transparent"><iframe width="960" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_1" name="aswift_1" style="left:0;position:absolute;top:0;"></iframe></ins></ins></ins><script>(adsbygoogle=window.adsbygoogle || []).push({});</script></div>
</div>  

<!-- 广告位开始 -->
<div class="J_adv" data-view="true" data-mod="ad_popu_72" data-mtp="62" data-order="40" data-con="ad_content_2072"><script id="popuLayer_js_q" src="http://ads.csdn.net/js/popuLayer.js" defer="" type="text/javascript"></script><div id="layerd" style="position: fixed; bottom: 0px; right: 0px; line-height: 0px; z-index: 1000; width: 300px; height: 278px; display: none;"><div class="J_close layer_close" style="display:;background-color:#efefef;padding:0px;color:#333;font:12px/24px Helvetica,Tahoma,Arial,sans-serif;text-align:right;">关闭</div><!-- 广告占位容器 --><div id="cpro_u2895327"><iframe id="iframeu2895327_0" src="http://pos.baidu.com/ncrm?rdid=2895327&amp;dc=3&amp;di=u2895327&amp;dri=0&amp;dis=0&amp;dai=1&amp;ps=638x1049&amp;dcb=___adblockplus&amp;dtm=HTML_POST&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1488201643501&amp;ti=Cookie%E8%B7%A8%E5%9F%9FsetDomain%E5%92%8CsetPath%20-%20zhyh1435589631%E7%9A%84%E4%B8%93%E6%A0%8F%20-%20%E5%8D%9A%E5%AE%A2%E9%A2%91%E9%81%93%20-%20CSDN.&amp;ari=2&amp;dbv=2&amp;drs=3&amp;pcs=1349x662&amp;pss=1349x3891&amp;cfv=0&amp;cpl=5&amp;chi=1&amp;cce=true&amp;cec=UTF-8&amp;tlm=1488201643&amp;rw=662&amp;ltu=http%3A%2F%2Fblog.csdn.net%2Fzhyh1435589631%2Farticle%2Fdetails%2F51999915&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DW5QtD3Si_irlg01tx2POpWpu9Z69wChddG3xQ7c2sLb4DgvifKOTuxGEn7vdx6L-WpeKkqkzpqIIUeLECI-oXcVh8sq_PVoLrPecdESJJSS%26wd%3D%26eqid%3D819484dc000580620000000358b4275d&amp;ecd=1&amp;par=1366x728&amp;pis=-1x-1&amp;ccd=24&amp;cja=false&amp;cmi=7&amp;col=zh-CN&amp;cdo=-1&amp;sr=1366x768&amp;tcn=1488201644&amp;qn=51dd0b80e1c30f74&amp;tt=1488201643481.23.23.27" width="300" height="250" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" style="border:0; vertical-align:bottom;margin:0;" allowtransparency="true"></iframe></div></div><script>  document.getElementById('popuLayer_js_q').onload=function(){      var styObjd=styObj={width:'300px','height':parseInt(250)+28};window.CSDN.Layer.PopuLayer('#layerd',{storageName:'layerd',styleObj:styObjd,total:50,expoire:1000*60});  }</script><!-- 投放代码 --><script type="text/javascript">   /*服务器频道首页置顶Banner960*90,创建于2014-7-3*/    (window.cproArray = window.cproArray || []).push({        id: 'u2895327'      });  </script>  <script src="http://cpro.baidustatic.com/cpro/ui/c.js" type="text/javascript"></script></div>
<!-- 广告位结束 -->
查看评论

  暂无评论

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
<div id="ad_bot">
</div>
    <a id="quick-reply" class="btn btn-top q-reply" title="快速回复" style="display:none;">
        <img src="http://static.blog.csdn.net/images/blog-icon-reply.png" alt="快速回复">
    </a>    
<a id="d-top-a" class="btn btn-top backtop" style="display: none;" title="返回顶部" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_huidaodingbu'])">         
     <img src="http://static.blog.csdn.net/images/top.png" alt="TOP">
</a>

                    <div class="clear">
                    </div>
                </div>
posted @ 2017-02-27 21:32  jobs-lgy  阅读(3461)  评论(0编辑  收藏  举报