Cam*_*ron 13 rest jquery cakephp
我正在尝试使用REST插件为我的CakePHP应用程序构建一个API:https://github.com/kvz/cakephp-rest-plugin
使用此插件的原因是因为我想要一种简单的方法来处理有关受保护方法的API调用的身份验证,并且还希望将来处理日志记录等.
为了防止重复,插件的设置如上一个问题所述:CakePHP REST插件不返回现已修复并正常工作的数据.
下一部分是处理身份验证.我已经将以下内容添加到我的beforeFilter方法中,基本上应该说是否是一个休息调用并且用户没有登录(注意:并非所有方法都需要用户登录)然后将其登录.
if (!$this->Auth->user()) {
if ($this->Rest->isActive()) {
$loginUser = $this->User->loginUser(
$credentials['username'],
AuthComponent::password($credentials['password'])
);
if($loginUser) {
if (!$this->Auth->login($loginUser['User'])) {
$msg = sprintf('Unable to log you in with the supplied credentials. ');
return $this->Rest->abort(array('status' => '403', 'error' => $msg));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于那些想要看模型调用的人:
public function loginUser($usernameOrEmail, $password)
{
return $this->find('first', array(
'conditions' => array(
'OR' => array(
'User.email' => $usernameOrEmail,
'User.username' => $usernameOrEmail,
'User.phone' => $usernameOrEmail
),
'User.password' => $password
),
'recursive' => -1
));
}
Run Code Online (Sandbox Code Playgroud)
但是我无法让它发挥作用.我正在做这样的测试请求:
$.ajax({
url: 'http://domain.com/users/test.json',
dataType: 'jsonp',
data: { username: 'test', password: 'test' },
headers: {
Authorization: "TRUEREST"
},
success: function(response) {
console.log(resonse);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
}
});
Run Code Online (Sandbox Code Playgroud)
我在这里面临的三个主要问题是:
我在这里手动传递用户名和密码但是标题部分是否正确?关于它的解释在这里的文档相当模糊:https://github.com/kvz/cakephp-rest-plugin#authorization
是$credentials['username']
打算把它捡起来?$凭证是HTTP身份验证的全局内容还是我错过了什么?
这会在每次提出请求时记录用户吗?如果是这样的话似乎有点乱.
当前代码实现错误: Uncaught SyntaxError: Unexpected token :
这是JSON输出,对我来说很好......
{
"data": {
"User": []
},
"meta": {
"status": "error",
"feedback": [{
"message": "Log in to continue",
"level": "error"
}],
"request": {
"http_host": "domain.com",
"http_user_agent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit\/537.22 (KHTML, like Gecko) Chrome\/25.0.1364.29 Safari\/537.22",
"server_addr": "##.##.###.###",
"remote_addr": "##.###.###.###",
"server_protocol": "HTTP\/1.1",
"request_method": "GET",
"request_uri": "\/users\/test.json?callback=jQuery172015279368730261922_1358207116601&username=test&password=test123&_=1358207116611",
"request_time": 1358207116
},
"credentials": {
"class": null,
"apikey": null,
"username": null
},
"time_epoch": "1358207117",
"time_local": "Mon, 14 Jan 2013 15:45:17 -0800",
"version": "0.3"
}
}
Run Code Online (Sandbox Code Playgroud)
您的问题很多,涉及范围很广。我会指出我发现问题的所有方向。您可能发现的最大问题是找不到用户。这是因为除非您$credentials
在代码中的其他位置进行设置,否则它们不会被设置。
缺少凭证
我没有看到你$credentials
在任何地方设置变量。.ajax 中的数据选项作为 GET 请求传递。您可能需要从 更新$credentials['var']
到$_GET['var']
。$this->data->params['username']
如果我没记错的话,这些也可以在和中找到$this->data->params['password']
。
如果用户未经过身份验证怎么办
我看到你的if (!$this->Auth->user())
,我看到你试图在哪里提取用户信息,但逻辑似乎会允许未经授权的用户通过。您可能想多看看这个逻辑。这是我要做的:
if ($this->Rest->isActive()) {
$user = $this->User->loginUser(
$_GET['username'],
AuthComponent::password($_GET['password'])
);
if (!$this->Auth->login($user)) {
$msg = sprintf('Unable to log you in with the supplied credentials. ');
return $this->Rest->abort(array('status' => '403', 'error' => $msg));
}
} else {
// handle the non-rest request
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7147 次 |
最近记录: |