Angular 和 Mysql 数据库安全的 Slim 框架

Angular and Slim framework for Mysql database security

我是 Angular 和 Slim 数据 Api 的新手。 我使用 Angular 使用的 Slim 创建数据 Api。我的 api 代码如下:

include 'dbConfig.php';
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$slim_app = new \Slim\Slim();

$slim_app->get('/getUser','getUser');
$slim_app->post('/updateUser','updateUser');
$slim_app->post('/newUser','newUser');

$slim_app->run();


function getUser(){
    global $mysqli;
    $result = $mysqli->query("select * from users");
    if(!$result){ echo "not connect";}
    $rows = array();
    while($row = $result->fetch_assoc()){
     $rows[] = $row; 
    }
    //echo "<pre>";
    print json_encode($rows);
} 

我的控制器

myApp.controller('Ctrl',function ($scope, $http) {    

    $http.get('api/getUser').
    success(function(data, status, headers, config){
      $scope.obj = data;
    });
});

当我转到 localhost/api/getUser 时,我可以看到我所有的 json 数据,这意味着任何人都可以看到。我怎样才能只允许我的 Angular 应用程序使用我的 api 以使其安全?

对了,

这是一个复杂的主题,但在最简单的形式中,您需要一个登录路由,该登录路由 return 是您客户的 API 密钥,就像这样....

注意这是我用于测试目的的非常基本的代码,它缺少正确的密码散列和检查访问令牌过期以及相当多的其他安全性。只是为了说明一个简单的例子。

app->get('/login/:username/:password', function ($username, $password) use ($app, $db) {
    //check the login details against the db...
   // TODO Include hashing FIX BEFORE PRODUCTION 
    $db->where("userName",  $username);
    $db->where("password", $password);
    $result = $db->getOne('Users');

    if ($db->count === 0) {
        setResponseCode(401, array("Error" => "Incorrect login details."));
        return;
    }

    // The way this key is generated needs updating, only for testing purposes.
    $key = date("Y-m-d");
    $key = $key . $username;
    $key = md5($key);
    // Need to add expiry time to access tokens....
    $expire = date("Y-m-d H:i:s", strtotime('+15 minutes'));

     $data = Array ('accessToken'  => $key);

    $db->where('id', $result['id']);
    if (!$db->update('Users', $data)) {
        // Internal error - this shouldn't happen!
        setResponseCode(500, array("Error" => "Database error."));
        return;
    }
    // Slim by default returns 200 so no need to call setResponseCode
    echo json_encode(array("Authorisation" => $key));
});

function setResponseCode($code, $arrayJson) {
    global $app;
    /*
      Function to save repetition of response headers - params are:
      Int Code - HTTP Response Code
      Array array_json - an array to be serialized into a JSON response ie array("Key" => "Item")
     */
    $app->response->setStatus($code);
    $app->response->SetBody(json_encode($arrayJson));
    $app->response->headers->set('Content-Type', 'application/json');
}

function authorise($key) {
    // Middleware for routes that require auth!
    //global $key;
    global $userId;
    global $app;
    global $db;

    // need to add check for token expiry here!

    $db->where('accessToken', $key);
   $results = $db->getOne('Users', null, array('id'));

    if ($db->count === 0) {

        setResponseCode(401, array("Error" => "Authorisation Failure"));
        $app->stop();
        //return false;
    }

    $userId = $results['id'];
    return true;
}

您的客户端需要存储密钥并将其添加到您可以使用 headers 或仅使用简单参数的任何请求的参数中。我再次将 google 苗条、API、访问令牌、教程作为术语,因为这是对复杂主题的非常基本的回答。

现在您只需将 authorise($key) 添加到任何需要它的路由的顶部,如果它是 false,它将 return 401 并停止任何进一步的执行。我建议多读一些书,因为这是一个非常非常基本的答案。