ceeram / authenticate
CakePHP 插件,为 AuthComponent 提供认证类。
1.0.0
2013-09-12 09:44 UTC
Requires
- php: >=5.3.0
- composer/installers: *
This package is auto-updated.
Last update: 2024-09-13 09:47:01 UTC
README
包含一些认证类以用于 AuthComponent 的插件。
当前类
- MultiColumnAuthenticate,允许在单个用户名字段中使用多个数据库列进行登录,例如用户名或电子邮件
- CookieAuthenticate,使用cookie进行登录
- TokenAuthenticate,使用作为URL参数或头部的令牌进行登录
GoogleAuthenticate 已移动到独立的仓库:[https://github.com/ceeram/GoogleAuthenticate](https://github.com/ceeram/GoogleAuthenticate)
需求
- PHP 5.3
- CakePHP 2.x
安装
[Composer]
运行: composer require friendsofcake/authenticate
或在您的应用程序的 composer.json
中的 require
部分添加 friendsofcake/authenticate
[手动安装]
- 下载此文件:[http://github.com/FriendsOfCake/Authenticate/zipball/master](http://github.com/FriendsOfCake/Authenticate/zipball/master)
- 解压下载的文件。
- 将生成的文件夹复制到 app/Plugin
- 将您刚刚复制的文件夹重命名为 Authenticate
[GIT 子模块]
在您的应用程序目录中输入
git submodule add git://github.com/FriendsOfCake/Authenticate.git Plugin/Authenticate
git submodule init
git submodule update
[GIT 克隆]
在您的插件目录中输入 git clone git://github.com/FriendsOfCake/Authenticate.git Authenticate
用法
在 app/Config/bootstrap.php
中添加: CakePlugin::load('Authenticate')
;
配置
设置认证类设置
MultiColumnAuthenticate
//in $components public $components = array( 'Auth' => array( 'authenticate' => array( 'Authenticate.MultiColumn' => array( 'fields' => array( 'username' => 'login', 'password' => 'password' ), 'columns' => array('username', 'email'), 'userModel' => 'User', 'scope' => array('User.active' => 1) ) ) ) ); //Or in beforeFilter() $this->Auth->authenticate = array( 'Authenticate.MultiColumn' => array( 'fields' => array( 'username' => 'login', 'password' => 'password' ), 'columns' => array('username', 'email'), 'userModel' => 'User', 'scope' => array('User.active' => 1) ) );
CookieAuthenticate
//in $components public $components = array( 'Auth' => array( 'authenticate' => array( 'Authenticate.Cookie' => array( 'fields' => array( 'username' => 'login', 'password' => 'password' ), 'userModel' => 'SomePlugin.User', 'scope' => array('User.active' => 1) ) ) ) ); //Or in beforeFilter() $this->Auth->authenticate = array( 'Authenticate.Cookie' => array( 'fields' => array( 'username' => 'login', 'password' => 'password' ), 'userModel' => 'SomePlugin.User', 'scope' => array('User.active' => 1) ) );
同时设置两者
它将首先尝试读取cookie,如果失败,将尝试使用表单数据
//in $components public $components = array( 'Auth' => array( 'authenticate' => array( 'Authenticate.Cookie' => array( 'fields' => array( 'username' => 'login', 'password' => 'password' ), 'userModel' => 'SomePlugin.User', 'scope' => array('User.active' => 1) ), 'Authenticate.MultiColumn' => array( 'fields' => array( 'username' => 'login', 'password' => 'password' ), 'columns' => array('username', 'email'), 'userModel' => 'User', 'scope' => array('User.active' => 1) ) ) ) );
安全
为了增强安全性,如果您打算使用Cookie认证,请确保将以下代码添加到您的 AppController::beforeFilter()
public function beforeFilter() { $this->Cookie->type('rijndael'); //Enable AES symetric encryption of cookie }
设置cookie
设置cookie的示例
<?php App::uses('AppController', 'Controller'); /** * Users Controller * * @property User $User */ class UsersController extends AppController { public $components = array('Cookie'); public function beforeFilter() { $this->Cookie->type('rijndael'); } public function login() { if ($this->Auth->loggedIn() || $this->Auth->login()) { $this->_setCookie(); $this->redirect($this->Auth->redirect()); } } protected function _setCookie() { if (!$this->request->data('User.remember_me')) { return false; } $data = array( 'username' => $this->request->data('User.username'), 'password' => $this->request->data('User.password') ); $this->Cookie->write('User', $data, true, '+1 week'); return true; } public function logout() { $this->Auth->logout(); $this->Session->setFlash('Logged out'); $this->redirect($this->Auth->redirect('/')); } }
TokenAuthenticate
//in $components public $components = array( 'Auth' => array( 'authenticate' => array( 'Authenticate.Token' => array( 'parameter' => '_token', 'header' => 'X-MyApiTokenHeader', 'userModel' => 'User', 'scope' => array('User.active' => 1), 'fields' => array( 'username' => 'username', 'password' => 'password', 'token' => 'public_key', ), 'continue' => true ) ) ) ); //Or in beforeFilter() $this->Auth->authenticate = array( 'Authenticate.Token' => array( 'parameter' => '_token', 'header' => 'X-MyApiTokenHeader', 'userModel' => 'User', 'scope' => array('User.active' => 1), 'fields' => array( 'username' => 'username', 'password' => 'password', 'token' => 'public_key', ), 'continue' => true ) );