【问题标题】:same origin policy not working同源政策不起作用
【发布时间】:2014-05-03 22:37:42
【问题描述】:

我试图通过我创建的一个小演示来了解同源策略。但不知何故出了点问题。以下是 2 个不同域(我在 XAMP 中托管的虚拟域)上的 html 文件:-

domain1.com

<html>
<title>
 DOMAIN1.COM
</title>
<script>
 function showTheirSecret() 
 {
var   stolenSecret=document.getElementById('stealSecret').contentWindow.document.getElementsByName("mySecret")[0].value;
if (stolenSecret)
{
    alert("Script on this page accessed the secret box and says "+stolenSecret);
}
else
    alert("Script on this page can not access the secret box!! ");  
}
</script>
<body>
  WELCOME TO <h1>domain1.com</h1><br>
  This is the contents on domain1.com. <br>
  These can not be accessed by domain2.com
  <br>
  <br>
  <iframe id="stealSecret"  src="http://localhost/~user/training/domain2.com/"></iframe>
  <br>
  <br>
  <h2>
  Click the "ok" button to see domain 2's secret text.
  </h2>
  <input type="button" value="stealData" onclick="javascript:showTheirSecret()">
  </body>
</html>

domain2.com

<html>
<title>
  DOMAIN2.COM
</title>
<script type="text/javascript">
function showMe() 
{
var secret=document.getElementsByName("mySecret")[0].value;
if(secret)
{
    alert("Script on this page accessed the secret box and says "+secret);
}
else
    alert("Script on this page can not access the secret box!! ");
}
 </script>
 <body>
   WELCOME TO <h1>domain2.com</h1><br>
   This is the contents on domain2.com. <br>
   These can not be accessed by domain1.com
   <br>
   <h2> 
   Put your secret text here !! 
   </h2> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
   <h2>
Click the "ok" button to see your own text.
</h2>
<input type="password" name="mySecret" value ="">
<input type="button" value="ok" onclick="javascript:showMe()">
</body>

现在假设我在 domain1.com 和 iframe(包含 domain2.com)中,我在 iframe 的文本框中输入了一些文本。现在我点击“stealData”按钮。所以理想情况下,我在这里期望的是相同的来源策略应该启动,并且我不应该被允许访问 iframe 中文本框的内容。同样应该在 Firefox 的 java 脚本控制台中显示为错误。但这并没有真正发生。为什么?

【问题讨论】:

  • 在你谈论“domain1.com”和“domain2.com”的问题中,但在你的HTML中你有&lt;iframe src="http://localhost/... - 你确定你实际上是在提供来自不同域的两个文件?
  • 好吧,我想我也可能是错的。我是这么怀疑的。但是,我不确定如何在 XAMPP 上的 Mac 中获取 2 个不同的虚拟域。我按照这里的步骤操作:- f6design.com/journal/2012/03/11/… 但是,我不确定它是否对我有用。我是 Mac 和 XAMP 的新手。任何帮助将不胜感激。

标签: javascript html cross-domain same-origin-policy


【解决方案1】:

谢谢大家。经过 RichieHIndle 的评论后,我意识到设置域本身是一个错误。我的域的 httpd-vhosts.conf 条目不正确。纠正这个文件完成了工作,我得到了我所期望的。我可以看到相同的原产地政策在起作用。

【讨论】:

    猜你喜欢
    • 2017-04-15
    • 2011-08-17
    • 2018-07-23
    • 2016-05-22
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多