<p>根据您的基础设施,@dragonx的答案可能会让您最感兴趣。在</p>
<p>我的2c</p>
<p>你想确保只有当客户访问你的网站时才能使用api?嗯,机器人,机器人,爬虫和客户端属于同一类吗?还是我错了?这很容易被利用,如果你真的想保护它。在</p>
<blockquote>
<p>I cannot believe I'm the only person with this requirement.</p>
</blockquote>
<p>也许不是,但正如您所见,您的API容易受到几次攻击,这可能是有人不共享您的设计并使auth的安全性更严格的原因。在</p>
<h2>编辑</h2>
<p>既然我们讨论的是AJAX请求,那么IP部分与此有什么关系?IP<strong>将始终是客户的IP</strong>!所以,你可能需要一个公共API。。。在</p>
<ul>
<li><p>我将使用令牌/会话/cookie部分。</p></li>
<li><p>我将使用一个生成的令牌,该令牌可以持续一段时间,并使用下面描述的流。</p></li>
<li><p>每隔一段时间我会用限制器,<a href="http://developer.github.com/v3/#rate-limiting" rel="nofollow noreferrer">like Github does</a>。例如每个ip每小时请求60个或更多个请求<a href="http://developer.github.com/v3/rate_limit/" rel="nofollow noreferrer">registered users</a></p></li>
</ul>
<p>为了克服刷新令牌的问题,我只需执行以下操作:</p>
<ol>
<li><p>客户访问现场</p>
<p>->服务器生成<strong>API令牌初始化</strong></p>
<p>->客户端获取API令牌INIT,该令牌仅对启动1个请求有效。</p></li>
<li><p>客户端向API发出AJAX请求</p>
<p>->客户端使用API令牌初始化</p>
<p>->服务器检查API令牌初始化和限制</p>
<p>->服务器接受请求</p>
<p>->服务器传回API令牌</p>
<p>->客户端使用响应数据并存储<strong>API令牌</strong>以供进一步使用(将通过JS存储在浏览器内存中)</p></li>
<li><p>客户机在有限的时间或请求量内启动与API的通信。请注意,您还知道init token<strong>日期</strong>,因此您可以使用它来检查页面上的第一次访问。</p></li>
</ol>
<p>当客户端访问时,第一个令牌通过服务器生成。
然后,客户机使用该令牌来获得一个真正的令牌,该令牌将持续一段时间或其他限制。
也许有人可以限制这个网页的访问量</p>
<p>这样你就不需要提神了。在</p>
<p>当然,上面的场景可以简化为只有一个令牌和上面提到的时间限制。在</p>
<p>当然,由于您没有身份验证,所以上面的场景很容易使用高级爬虫程序等。在</p>
<p>当然,聪明的攻击者可以从服务器上获取令牌并重复这些步骤,但是,从一开始就已经存在这个问题。在</p>
<p>一些额外的分数</p>
<ul>
<li>根据提供的注释,请关闭对API的写入。如果您对自己的实现(如果不使用auth)或额外的安全性有疑问,您不希望成为DOS攻击的受害者</li>
<li>如上所述的令牌场景也可能变得更加复杂,例如通过不断地交换令牌</li>
</ul>
<p>仅供参考,GAE云存储使用<a href="https://developers.google.com/storage/docs/accesscontrol#Signed-URLs" rel="nofollow noreferrer">signed_urls</a>来达到相同的目的。在</p>
<p>希望有帮助。在</p>
<p>关于IP欺骗和防范欺骗攻击,<strong>维基百科</strong>称不会将数据包返回给攻击者:</p>
<blockquote>
<p>Some upper layer protocols provide their own defense against IP
spoofing attacks. For example, Transmission Control Protocol (TCP)
uses sequence numbers negotiated with the remote machine to ensure
that arriving packets are part of an established connection. Since the
attacker normally can't see any reply packets, the sequence number
must be guessed in order to hijack the connection. The poor
implementation in many older operating systems and network devices,
however, means that TCP sequence numbers can be predicted.</p>
</blockquote>