首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PhoneGap允许(Ajax请求)服务器端只访问此应用程序的文件(安全目的)

PhoneGap允许(Ajax请求)服务器端只访问此应用程序的文件(安全目的)
EN

Stack Overflow用户
提问于 2017-09-28 14:19:12
回答 2查看 370关注 0票数 -3

我已经使用PhoneGap创建了一个安卓应用程序(PhoneGap是一个工具,它支持您使用HTML、CSS和JavaScript等语言创建应用程序)。

请注意,android应用程序是一个开源应用程序。因此android应用程序代码对服务器是公开的。所以我不能把密码,验证变量传给服务器。

这是我的代码:

<script type="text/javascript">
    $(document).ready(function() {
        $("#insert").click(function() {
            var title = $("#title").val();
            var duration = $("#duration").val();
            var price = $("#price").val();
            var dataString = "title=" + title + "&duration=" + duration + "&price=" + price + "&insert=";
            if ($.trim(title).length > 0 & $.trim(duration).length > 0 & $.trim(price).length > 0) {
                $.ajax({
                    type: "POST",
                    url: "http://www.example.com/test/insert.php",
                    data: dataString,
                    crossDomain: true,
                    cache: false,
                    beforeSend: function() {
                        $("#insert").val('Connecting...');
                    },
                    success: function(data) {
                        if (data == "success") {
                            alert("inserted");
                            $("#insert").val('submit');
                        } else if (data == "error") {
                            alert("error");
                        }
                    }
                });
            }
            return false;
        });
    });
    </script>

此函数应使用http://www.example.com/test/insert.php更新服务器上的表。当应用程序安装在android手机上时,它工作得很好,没有任何问题。

但是其他用户可以使用上面的功能很容易地更新我的表格。我的意思是,如果他们知道网址(http://www.example.com/test/insert.php),他们也可以通过必要的post请求来更新我的表。

如何防止这种情况发生?我如何才能允许访问仅限于我的应用程序的页面http://www.example.com/test/insert.php。我的意思是,如果请求是从我的应用程序到http://www.example.com/test/insert.php,它应该可以工作。

EN

回答 2

Stack Overflow用户

发布于 2018-06-04 20:54:45

单击insert按钮时,尝试在隐藏输入变量中发送一个值。在您的服务器端编程中,使用下面这样的变量进行检查

if($_POST['insert'] && $_POST['<hidden varaible name>'])
{
 //your code goes here ...
}
票数 0
EN

Stack Overflow用户

发布于 2018-06-04 20:59:55

我认为你需要像this这样的东西

Android端:

SECRET_KEY = "abc123"

def call_api_with_secret(url, params)
  # create the hash to sign the request
  hash = MD5.hash(SECRET_KEY, url, params)

  # call the api with the added hash
  call_api(url+"&hash=#{hash}", params)
end

服务器端:

    SECRET_KEY = "abc123"

    def receive_from_api(url, params)
      # retrieve the hash
      url_without_hash, received_hash = retrieve_and_remove_hash(url)

      # check the hash
      expected_hash = MD5.hash(SECRET_KEY, url_without_hash, params)

      if (expected_hash != received_hash)
        raise our exception!
      end

      # now do the usual stuff

end
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46462212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档