简体   繁体   English

Web服务和phonegap:最佳实践

[英]web services and phonegap : best practices

Hi I am using phonegap for crossed plateform development (I use angularJS as JS framework). 嗨我正在使用phonegap进行交叉平台开发(我使用angularJS作为JS框架)。 I want to use a web service to access to a list of positions from my database (mysql) on my website. 我想使用Web服务从我的网站上的数据库(mysql)访问位置列表。

The problem is that the solution I found is not secure at all: 问题是我找到的解决方案根本不安全:

Javascript 使用Javascript

var xhr;
if (window.XMLHttpRequest)
    xhr =  new XMLHttpRequest();
else
    xhr =  ActiveXObject("Microsoft.XMLHTTP");

xhr.open("GET", "http://localhost:8888/MAMP_Site/0/test.php", true);
xhr.send(null);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
    console.log("Ready State4: Json Textual Data retrieved");
          handleData(xhr.responseText); // Json Textual Data
    }
};

function handleData(data)
{
var jsonData;
console.log("ReceivedData from WebService:"+data);
jsonData = eval('(' + data + ')');

$scope.lastUpdate = jsonData[0];
$scope.jsonData = jsonData[1];
$scope.$apply();
}

PHP (used as "web service") PHP(用作“Web服务”)

<?php
    header('Access-Control-Allow-Origin: *');
    header("Content-Type: text/plain");

    class UserInfo {
        public $id = "";
        public $name  = "";
        public $username = "";
        public $timestamp = "";

        public function __construct($_id, $_name, $_username, $_timestamp) {
        $this->id = $_id;
        $this->name = $_name;
        $this->username = $_username;
        $this->timestamp = $_timestamp;
      }
    }


    $db = mysql_connect('localhost:8889', 'root', 'root'); 
    mysql_select_db('myDbName',$db);
    $sql = 'SELECT id,name,username,timestamp FROM positions_test'; 
    $req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
    $dataArray = array();

    while($data = mysql_fetch_assoc($req)) { 
        $dataArray[]= new UserInfo($data['id'],$data['name'],$data['username'],$data['timestamp']);
    } 

    //Last Modified Time
    $sql = "SELECT UPDATE_TIME FROM information_schema.tables WHERE  TABLE_SCHEMA = 'myDbName'AND TABLE_NAME = 'positions_test'"; 
    $req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
    $data = mysql_fetch_assoc($req)["UPDATE_TIME"]; 

    $jsonDataArray = array($data, $dataArray);
    echo json_encode($jsonDataArray);
    mysql_close(); 
?>


Basically the PHP return a JSON (as text), and I get it (as text) in my JS. 基本上PHP返回一个JSON(作为文本),我在我的JS中得到它(作为文本)。 Then I evaluate it as a JSON. 然后我将它评估为JSON。

The question 这个问题

Security concern 安全问题

As the application is made with cordova, all JS and Html source code can be viewed and so the URL of my php "web service". 由于应用程序是使用cordova制作的,因此可以查看所有JS和Html源代码,以及我的php“web服务”的URL。 It means that anybody who have the adress can access to the Json File. 这意味着拥有地址的任何人都可以访问Json文件。 Even if this data is public (in my case) I want it to be only accessible from my app (this way I can for instance avoid a bot to store all of this data and spam). 即使这些数据是公开的(在我的情况下),我希望它只能从我的应用程序访问(这样我可以避免机器人存储所有这些数据和垃圾邮件)。

Token or user-agent 令牌或用户代理

As there is no authentification for users is there any way for my webservice to know where the request come from? 由于没有对用户进行身份验证,我的网络服务有什么方法可以知道请求的来源?
I thought using a token to ensure that the request come from my app but once again as the source code can be viewed, anybody could see the token or the code to generate it. 我想使用令牌来确保请求来自我的应用程序但是再一次可以查看源代码,任何人都可以看到令牌或代码来生成它。
Maybe using user-agent to know if it is accessed from a mobile device? 也许使用用户代理知道它是否是从移动设备访问的?

Other port than 80 其他端口比80

Maybe it would be judicious to choose another port than 80 to connect to my web service, but how can I select my connexion port? 也许选择80以外的其他端口连接到我的Web服务是明智的,但是如何选择我的连接端口呢?

Best practice 最佳实践

The main point would actually be, what are the best practice for web services on phonegap (cordova) ? 实际上,重点是,phonegap(cordova)上的Web服务的最佳实践是什么? Should I use SSL, Https? 我应该使用SSL,Https吗?
Should I use a real web service instead of a simple php page and XMLHTTPRequest? 我应该使用真正的Web服务而不是简单的php页面和XMLHTTPRequest吗? If yes, which one? 如果是的话,哪一个?
And of course how building properly and securely my web service ? 当然,如何正确安全地构建我的Web服务?

I know this is a long post, but I searched the web a for while and I found a lot of interesting stuff but nothing really concret on the best practices to build your web services for a phonegap application (with no user authentification) 我知道这是一个很长的帖子,但我在网上搜索了一段时间,我发现了许多有趣的东西,但没有真正具体的建立你的webgap应用程序的web服务的最佳实践(没有用户身份验证)

You could try to obfuscate it, or aa lot of other things, but in the end you have to receive it in the client side and therefore there is nothing you can do to fully prevent him from reading your data, seeing your client side code or spamming your service. 您可以尝试对其进行模糊处理,或者许多其他事情,但最终您必须在客户端接收它,因此您无法完全阻止他阅读您的数据,查看客户端代码或垃圾邮件你的服务。

The best you can do to make sure that the service is safe is: make sure the connection to the db does not allow writes, all the software involved is updated regularly and that the queries sent to your service have the syntax and content that you are expecting. 确保服务安全的最佳方法是:确保与数据库的连接不允许写入,所有涉及的软件都会定期更新,并且发送到您服务的查询具有您的语法和内容期待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM