阅读跨域cookie

时间:2014-05-08 13:02:02

标签: java javascript jsf-2

javascript / JSF可以从其他域读取cookie吗?

如果我在一个域中设置cookie,请说www.domain1.com。我可以从其他域名www.domain2.com上阅读该cookie吗?

如果是,怎么样?

我尝试了下面的代码,但它没有从提到的网址中读取Cookie值,即http://host.example.com

public void getCookieUsingCookieHandler() { 
try {       
    // Instantiate CookieManager;
    // make sure to set CookiePolicy
    CookieManager manager = new CookieManager();
    manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(manager);

    // get content from URLConnection;
    // cookies are set by web site
    URL url = new URL("http://host.example.com");
    URLConnection connection = url.openConnection();
    connection.getContent();

    // get cookies from underlying
    // CookieStore
    CookieStore cookieJar =  manager.getCookieStore();
    List <HttpCookie> cookies =
        cookieJar.getCookies();
    for (HttpCookie cookie: cookies) {
      System.out.println("CookieHandler retrieved cookie: " + cookie);
    }
} catch(Exception e) {
    System.out.println("Unable to get cookie using CookieHandler");
    e.printStackTrace();
}

}

1 个答案:

答案 0 :(得分:0)

这是完全不安全的,不可能。 epascarello击中头部。如果您可以从任何域读取cookie,那么您就可以从刚刚打开网页的用户那里窃取会话cookie。