如何防止直接访问我的JSON服务?

编程入门 行业动态 更新时间:2024-10-14 20:20:56
本文介绍了如何防止直接访问我的JSON服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个JSON网络服务来返回我的Google地图上显示的家庭标记。

I have a JSON web service to return home markers to be displayed on my Google Map.

基本上, http://示例。 com 调用Web服务以找出所有地图标记的位置,如下所示:

Essentially, example calls the web service to find out the location of all map markers to display like so:

example/json/?zipcode=12345

它返回一个JSON字符串,如as:

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.

我只想 example/index.html 才能打电话我的 example/json/ 网络服务...而不是一些随机的家伙打电话给 / json / 直接。

I only want example/index.html to be able to call my example/json/ web service ... and not some random dude calling the /json/ directly.

Quesiton :我该如何防止直接c所有/访问我的 example/json/ 网络服务?

Quesiton: how do I prevent direct calling/access to my example/json/ web service?

更新

为了更清晰, example/ index.html 调用 example/json/?zipcode=12345 ...和JSON服务 - 返回半敏感数据, - 返回一个JSON数组, - 响应GET请求, - 浏览器制作该请求已启用JavaScript

To give more clarity, example/index.html call example/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。您必须提供cookie。
  • 通过您发明的自定义HTTP标头。
  • By IP address. In Apache, use the Allow / Deny directives.
  • By HTTP auth: basic or digest. This is nice and standardized, and uses usernames/passwords to authenticate.
  • By cookie. You'll have to come up with the cookie.
  • By a custom HTTP header that you invent.

编辑:

我一开始并没有意识到客户端代码正在调用您的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服务?

本文发布于:2023-11-01 13:25:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何防止   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!