friendsofcake / authenticate
该包已被废弃,不再维护。没有建议的替代包。
CakePHP 插件,用于 AuthComponent 的认证类。
2.0.0
2015-12-22 21:34 UTC
Requires
- php: >=5.4.0
- cakephp/cakephp: ~3.0
Requires (Dev)
- phpunit/phpunit: 4.1.*
This package is not auto-updated.
Last update: 2022-02-01 12:27:09 UTC
README
注意:该项目已不再积极维护。
Authenticate 类已经过时或出现了更好的替代方案
- MultiColumnAuthenticate,请参阅 工具 - 或者使用 CakePHP 3 的自定义查找器
- CookieAuthenticate,请参阅 Xety/Cake3-CookieAuth
- TokenAuthenticate,请参阅 JwtAuth
包含一些用于 AuthComponent 的认证类的插件。
当前类
- MultiColumnAuthenticate,允许使用单个用户名字段中的多个数据库列进行登录。例如用户名或电子邮件
- CookieAuthenticate,使用 cookie 进行登录
- TokenAuthenticate,使用作为 URL 参数或头的令牌进行登录
需求
- CakePHP 3.0
安装
[Composer]
运行:composer require friendsofcake/authenticate:dev-cake3
或在您的应用程序的 composer.json
中的 require
部分添加 "friendsofcake/authenticate":"dev-cake3"
。
用法
在您的应用程序的 config/bootstrap.php
中添加:Plugin::load('FOC/Authenticate');
配置
设置认证类设置
MultiColumnAuthenticate
//in $components public $components = [ 'Auth' => [ 'authenticate' => [ 'FOC/Authenticate.MultiColumn' => [ 'fields' => [ 'username' => 'login', 'password' => 'password' ], 'columns' => ['username', 'email'], 'userModel' => 'Users', 'scope' => ['Users.active' => 1] ] ] ] ]; // Or in beforeFilter() $this->Auth->config('authenticate', [ 'FOC/Authenticate.MultiColumn' => [ 'fields' => [ 'username' => 'login', 'password' => 'password' ], 'columns' => ['username', 'email'], 'userModel' => 'Users', 'scope' => ['Users.active' => 1] ] ]);
CookieAuthenticate
//in $components public $components = [ 'Auth' => [ 'authenticate' => [ 'FOC/Authenticate.Cookie' => [ 'fields' => [ 'username' => 'login', 'password' => 'password' ], 'userModel' => 'SomePlugin.Users', 'scope' => ['User.active' => 1] ] ] ] ]; //Or in beforeFilter() $this->Auth->authenticate = [ 'FOC/Authenticate.Cookie' => [ 'fields' => [ 'username' => 'login', 'password' => 'password' ], 'userModel' => 'SomePlugin.Users', 'scope' => ['Users.active' => 1] ] ];
设置两者
它将首先尝试读取 cookie,如果失败,则尝试使用表单数据
//in $components public $components = [ 'Auth' => [ 'authenticate' => [ 'FOC/Authenticate.Cookie' => [ 'fields' => [ 'username' => 'login', 'password' => 'password' ], 'userModel' => 'SomePlugin.Users', 'scope' => ['User.active' => 1] ], 'FOC/Authenticate.MultiColumn' => [ 'fields' => [ 'username' => 'login', 'password' => 'password' ], 'columns' => ['username', 'email'], 'userModel' => 'Users', 'scope' => ['Users.active' => 1] ] ] ] ];
设置 cookie
设置 cookie 的示例
<?php App::uses('AppController', 'Controller'); /** * Users Controller * * @property User $User */ class UsersController extends AppController { public $components = ['Cookie']; public function login() { if ($this->request->is('post')) { $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user); $this->_setCookie(); return $this->redirect($this->Auth->redirectUrl()); } $this->Flash->error(__('Invalid username or password, try again')); } } protected function _setCookie() { if (!$this->request->data('remember_me')) { return false; } $data = [ 'username' => $this->request->data('username'), 'password' => $this->request->data('password') ]; $this->Cookie->write('RememberMe', $data, true, '+1 week'); return true; } }
TokenAuthenticate
//in $components public $components = [ 'Auth' => [ 'authenticate' => [ 'FOC/Authenticate.Token' => [ 'parameter' => '_token', 'header' => 'X-MyApiTokenHeader', 'userModel' => 'Users', 'scope' => ['Users.active' => 1], 'fields' => [ 'username' => 'username', 'password' => 'password', 'token' => 'public_key', ], 'continue' => true ] ] ] ]; //Or in beforeFilter() $this->Auth->config('authenticate', [ 'FOC/Authenticate.Token' => [ 'parameter' => '_token', 'header' => 'X-MyApiTokenHeader', 'userModel' => 'Users', 'scope' => ['Users.active' => 1], 'fields' => [ 'username' => 'username', 'password' => 'password', 'token' => 'public_key', ], 'continue' => true ] ]);