问题描述
我有一个 JSON 网络服务来返回主页标记以显示在我的 Google 地图上.
I have a JSON web service to return home markers to be displayed on my Google Map.
本质上,http://example.com
调用 Web 服务来找出所有要显示的地图标记的位置,如下所示:
Essentially, http://example.com
calls the web service to find out the location of all map markers to display like so:
http://example.com/json/?zipcode=12345
它返回一个 JSON 字符串,例如:
And it returns a JSON string such as:
{"address": "321 Main St, Mountain View, CA, USA", ...}
所以在我的 index.html
页面上,我使用该 JSON 字符串并放置地图标记.
So on my index.html
page, I take that JSON string and place the map markers.
但是,我不希望人们直接调用我的 JSON 网络服务.
However, what I don't want to have happen is people calling out to my JSON web service directly.
我只希望 http://example.com/index.html
能够调用我的 http://example.com/json/
网络服务... 而不是一些随机的家伙直接调用 /json/
.
I only want http://example.com/index.html
to be able to call my http://example.com/json/
web service ... and not some random dude calling the /json/
directly.
问题:如何防止直接调用/访问我的 http://example.com/json/
网络服务?
Quesiton: how do I prevent direct calling/access to my http://example.com/json/
web service?
更新:
为了更清晰,http://example.com/index.html
调用 http://example.com/json/?zipcode=12345
... 和 JSON 服务
- 返回半敏感数据,
- 返回一个 JSON 数组,
- 响应 GET 请求,
- 发出请求的浏览器启用了 JavaScript
To give more clarity, http://example.com/index.html
call http://example.com/json/?zipcode=12345
... and the JSON service
- returns semi-sensitive data,
- returns a JSON array,
- responds to GET requests,
- the browser making the request has JavaScript enabled
同样,我不希望发生的是人们只需查看我的 index.html
源代码,然后直接调用 JSON 服务.
Again, what I don't want to have happen is people simply look at my index.html
source code and then call the JSON service directly.
推荐答案
有一些很好的方法来验证客户端.
There are a few good ways to authenticate clients.
- 按 IP 地址.在 Apache 中,使用 Allow/Deny 指令.
- 通过 HTTP 身份验证:基本或摘要.这很好且标准化,并使用用户名/密码进行身份验证.
- 通过 cookie.你必须拿出饼干.
- 通过您发明的自定义 HTTP 标头.
一开始我没有发现客户端代码正在调用您的 Web 服务.如果您让客户端 Javascript 执行此操作,实际上不可能阻止人们直接调用您的 Web 服务.有人可以阅读源代码.
I didn't catch at first that your web service is being called by client-side code. It is literally NOT POSSIBLE to prevent people from calling your web service directly, if you let client-side Javascript do it. Someone could just read the source code.
这篇关于如何防止直接访问我的 JSON 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!