目前我正在使用cakephp 3.1为Android应用程序开发一个安静的API。我一直在尝试使用ADmad / JwtAuth.Jwt组件,但我无法使其工作,我不知道为什么。我在没有使用CRUD组件的情况下跟踪了this tutorial。这是我的代码:
AppController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Auth', [
'storage' => 'Memory',
'authenticate' => [
'Form'=> ['fields' => ['username' => 'email', 'password' => 'password']],
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'fields' => [
'id' => 'id'
]
]
]
]);
}
UserContoller.php
use App\Controller\Api\AppController;
use Cake\Network\Exception\UnauthorizedException;
use Cake\Utility\Security;
use Cake\Event\Event;
class UsersController extends AppController
{
public function initialize()
{
parent::initialize();
$this->Auth->allow(['ea']);
}
public function ea()
{
$user = $this->Auth->identify();
if (!$user) {
throw new UnauthorizedException('Invalid username or password');
}
$this->set([
'success' => true,
'data' => [
'token' => $token = \JWT::encode([
'id' => $user['id'],
'exp' => time() + 604800
],
Security::salt())
],
'_serialize' => ['success', 'data']
]);
}
我使用函数ea()作为测试,看它是否有效。如果我在数据数组中设置$ user而不是token,它会显示用户信息没有问题,并且消息成功:true,但是当我尝试使用jwt :: encode()函数时,它会回复错误500.
我已经安装了最新版本的ADmad / JwtAuth.Jwt(composer info命令说dev-master 550c630)。在bootstrap.php中我添加了一行Plugin :: load('ADmad / JwtAuth');
我是cakephp的新手,所以这可能是一个愚蠢的错误。我花了一个多星期的时间试图自己解决这个问题,但是我已经没想完了。任何帮助将不胜感激。
PS:我使用邮递员客户端。 http post请求是
{ "email" : "fakeemail@gmail.com",
"password" : "pass"
}
更新1
我修复了以前的代码,但现在我在验证方面遇到了问题。这就是它现在的样子
AppController的
class AppController extends Controller{
public function initialize(){
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Auth', [
'authenticate', [
'ADmad/JwtAuth.Jwt' => [
'storage' => 'Memory',
'userModel' => 'Users',
'fields' => [
'username' => 'id'
],
'parameter' => '_token',
// Boolean indicating whether the "sub" claim of JWT payload
// should be used to query the Users model and get user info.
// If set to `false` JWT's payload is directly returned.
'queryDatasource' =>true,
]
],
'unauthorizedRedirect' => false,
'checkAuthIn' => 'Controller.initialize',
]);
}
UserController中
class UsersController extends AppController{
public function initialize(){
parent::initialize();
$this->Auth->allow([ 'token','add']);
}
public function token(){
$email = $this->request->data('email');
$pwd = $this->request->data('password');
$user = $this->Users->find()->where(['email' => $email])->first();
$token=null;
$success=false;
if($user != null && (new DefaultPasswordHasher)->check($pwd, $user['password'])){
$token = JWT::encode([
'id' => $user['id'],
'sub' => $user['id']
],Security::salt());
$succes=true;
}
$this->set([
'success' => $success,
'data' => [
'token' => $token
],
'_serialize' => ['success', 'data']
]);
}
函数token()工作正常。如果我在UserController的initialize()函数中允许index()函数也可以正常工作,但如果我不允许它并且我尝试从http请求中调用它,它会给我这个响应:
{
"message": "A route matching \"array (\n 'controller' => 'Users',\n 'action' => 'login',\n 'plugin' => NULL,\n 'prefix' => 'api',\n '_ext' => NULL,\n)\" could not be found.",
"url": "array (\n 'controller' => 'Users',\n 'action' => 'login',\n 'plugin' => NULL,\n 'prefix' => 'api',\n '_ext' => NULL,\n)",
"code": 404,
"trace": [
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Routing/Router.php",
"line": 617,
"function": "match",
"class": "Cake\Routing\RouteCollection",
"type": "->",
"args": [
{
"controller": "Users",
"action": "login",
"plugin": null,
"prefix": "api",
"_ext": null
},
{
"_base": "/deportes",
"_port": "80",
"_scheme": "http",
"_host": "localhost",
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api"
}
}
]
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Routing/Router.php",
"line": 730,
"function": "url",
"class": "Cake\Routing\Router",
"type": "::",
"args": [
{
"controller": "Users",
"action": "login",
"plugin": null
}
]
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Controller/Component/AuthComponent.php",
"line": 400,
"function": "normalize",
"class": "Cake\Routing\Router",
"type": "::",
"args": [
{
"controller": "Users",
"action": "login",
"plugin": null
}
]
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Controller/Component/AuthComponent.php",
"line": 290,
"function": "_isLoginAction",
"class": "Cake\Controller\Component\AuthComponent",
"type": "->",
"args": [
{
"name": "Users",
"helpers": [],
"request": {
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api",
"isAjax": false
},
"data": [],
"query": [],
"cookies": {
"CAKEPHP": "vaj518odrn96id8asjhpluob00"
},
"url": "api/users",
"base": "/deportes",
"webroot": "/deportes/",
"here": "/deportes/api/users",
"trustProxy": false
},
"response": {},
"paginate": [],
"autoRender": true,
"components": [],
"View": null,
"plugin": null,
"passedArgs": [],
"modelClass": "Users",
"viewClass": null,
"viewVars": [],
"RequestHandler": {
"enabled": true,
"response": {},
"ext": null,
"request": {
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api",
"isAjax": false
},
"data": [],
"query": [],
"cookies": {
"CAKEPHP": "vaj518odrn96id8asjhpluob00"
},
"url": "api/users",
"base": "/deportes",
"webroot": "/deportes/",
"here": "/deportes/api/users",
"trustProxy": false
},
"components": []
},
"Auth": {
"components": [
"RequestHandler",
"Flash"
],
"allowedActions": [
"token",
"add"
],
"request": {
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api",
"isAjax": false
},
"data": [],
"query": [],
"cookies": {
"CAKEPHP": "vaj518odrn96id8asjhpluob00"
},
"url": "api/users",
"base": "/deportes",
"webroot": "/deportes/",
"here": "/deportes/api/users",
"trustProxy": false
},
"response": {},
"session": {}
}
}
]
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Event/EventManager.php",
"line": 385,
"function": "authCheck",
"class": "Cake\Controller\Component\AuthComponent",
"type": "->",
"args": [
{
"data": null,
"result": null
}
]
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Event/EventManager.php",
"line": 355,
"function": "_callListener",
"class": "Cake\Event\EventManager",
"type": "->",
"args": [
[
{
"components": [
"RequestHandler",
"Flash"
],
"allowedActions": [
"token",
"add"
],
"request": {
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api",
"isAjax": false
},
"data": [],
"query": [],
"cookies": {
"CAKEPHP": "vaj518odrn96id8asjhpluob00"
},
"url": "api/users",
"base": "/deportes",
"webroot": "/deportes/",
"here": "/deportes/api/users",
"trustProxy": false
},
"response": {},
"session": {}
},
"authCheck"
],
{
"data": null,
"result": null
}
]
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Event/EventDispatcherTrait.php",
"line": 78,
"function": "dispatch",
"class": "Cake\Event\EventManager",
"type": "->",
"args": [
{
"data": null,
"result": null
}
]
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Controller/Controller.php",
"line": 491,
"function": "dispatchEvent",
"class": "Cake\Controller\Controller",
"type": "->",
"args": [
"Controller.initialize"
]
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Routing/Dispatcher.php",
"line": 109,
"function": "startupProcess",
"class": "Cake\Controller\Controller",
"type": "->",
"args": []
},
{
"file": "/var/www/html/deportes/vendor/cakephp/cakephp/src/Routing/Dispatcher.php",
"line": 87,
"function": "_invoke",
"class": "Cake\Routing\Dispatcher",
"type": "->",
"args": [
{
"name": "Users",
"helpers": [],
"request": {
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api",
"isAjax": false
},
"data": [],
"query": [],
"cookies": {
"CAKEPHP": "vaj518odrn96id8asjhpluob00"
},
"url": "api/users",
"base": "/deportes",
"webroot": "/deportes/",
"here": "/deportes/api/users",
"trustProxy": false
},
"response": {},
"paginate": [],
"autoRender": true,
"components": [],
"View": null,
"plugin": null,
"passedArgs": [],
"modelClass": "Users",
"viewClass": null,
"viewVars": [],
"RequestHandler": {
"enabled": true,
"response": {},
"ext": null,
"request": {
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api",
"isAjax": false
},
"data": [],
"query": [],
"cookies": {
"CAKEPHP": "vaj518odrn96id8asjhpluob00"
},
"url": "api/users",
"base": "/deportes",
"webroot": "/deportes/",
"here": "/deportes/api/users",
"trustProxy": false
},
"components": []
},
"Auth": {
"components": [
"RequestHandler",
"Flash"
],
"allowedActions": [
"token",
"add"
],
"request": {
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api",
"isAjax": false
},
"data": [],
"query": [],
"cookies": {
"CAKEPHP": "vaj518odrn96id8asjhpluob00"
},
"url": "api/users",
"base": "/deportes",
"webroot": "/deportes/",
"here": "/deportes/api/users",
"trustProxy": false
},
"response": {},
"session": {}
}
}
]
},
{
"file": "/var/www/html/deportes/webroot/index.php",
"line": 37,
"function": "dispatch",
"class": "Cake\Routing\Dispatcher",
"type": "->",
"args": [
{
"params": {
"plugin": null,
"controller": "Users",
"action": "index",
"_ext": null,
"pass": [],
"_method": "GET",
"prefix": "api",
"isAjax": false
},
"data": [],
"query": [],
"cookies": {
"CAKEPHP": "vaj518odrn96id8asjhpluob00"
},
"url": "api/users",
"base": "/deportes",
"webroot": "/deportes/",
"here": "/deportes/api/users",
"trustProxy": false
},
{}
]
}
]
}
Image from postman with the request
正如您在上图所示,我使用了Authorization标头。有什么想法吗?