我是 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);
}
我的 Controller
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 以使其安全?
最佳答案
对,
这是一个复杂的主题,但在最简单的形式中,您需要一个登录路由,将 API key 返回给您的客户端,如下所示......
注意,这是我用于测试目的的非常基本的代码,它缺少正确的密码散列和检查访问 token 过期以及相当多的其他安全性。这只是为了说明一个简单的例子。
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;
}
您的客户端需要存储 key 并将其添加到任何请求的参数中,您可以使用 header 或仅使用简单参数。我再次将 google slim、API、访问 token 、教程作为术语,因为这是对复杂主题的非常基本的答案。
现在你只需将authorise($key)添加到任何需要它的路由的顶部,如果它是假的,它将返回401并停止任何进一步的执行。我建议您多阅读一些内容,因为这是一个非常非常基本的答案。
关于php - 用于 Mysql 数据库安全的 Angular 和 Slim 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31508659/