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

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

我有一个 JSON 网络服务来返回主页标记以显示在我的 Google 地图上.

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

本质上,example 调用 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 字符串,例如:

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.

问题:如何防止直接调用/访问我的 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.你必须拿出饼干.
  • 通过您发明的自定义 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 服务?

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

发布评论

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

>www.elefans.com

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