crabstudio/authenticate

CakePHP 3.2+ 的高级认证插件

安装: 646

依赖者: 1

建议者: 3

安全: 0

星星: 2

关注者: 3

分支: 0

类型:cakephp-plugin

1.0.1 2016-08-03 00:00 UTC

This package is auto-updated.

Last update: 2024-08-29 04:19:53 UTC


README

Build Status Latest Stable Version Total Downloads License Latest Unstable Version

CakePHP 3.2+ 的高级认证插件

支持我 paypal

特性

  • 通过IP防止暴力破解攻击
  • 记住/自动登录

安装

您可以使用composer将此插件安装到您的CakePHP应用程序中。

安装composer包的推荐方法是

composer self-update
composer require crabstudio/authenticate

或者将以下行添加到应用程序的 composer.json

"require": {
    "crabstudio/authenticate": "^1.0"
}

然后执行以下命令

composer update

加载插件

从命令行

bin/cake plugin load Authenticate

或者将此行添加到文件末尾 Your_project\config\bootstrap.php

Plugin::load('Authenticate');

配置

AppController.php 配置Auth组件

// All config key as usual FormAuthenticate/BaseAuthenticate
// I list the different config keys only.

$this->loadComponent('Auth', [
    'authenticate' => [
        'Authenticate.Advance' => [
	        'lockout' => [
	            'retries' => 3,
	            'expires' => '5 minutes',
	            'file_path' => 'prevent_brute_force',
	            'message' => [
	                'locked' => 'You have exceeded the number of allowed login attempts. Please try again in {0}',
	                'login_fail' => 'Incorrect username or password. {0} retries remain. Please try again',
	            ]
	        ],
	        'remember' => [
	            'enable' => true,
	            'key' => 'RememberMe',
	            'expires' => '1 months',
	        ],
        ],
    ]);

如果您想将登录信息存储到Cookie中,登录表单需要checkbox RememberMe,如下所示

将此函数粘贴到 AppController.php

// remember to import Event to the AppController.php
use Cake\Event\Event;

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Cookie');
}

public function beforeFilter(Event $event)
{
    //Automaticaly Login.
    if (!$this->Auth->user() && $this->Cookie->read('RememberMe')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
        } else {
            $this->Cookie->delete('RememberMe');
        }
    }
}
// login.ctp template

<?=$this->Form->create()?>
<?=$this->Flash->render()?>
<?=$this->Form->input('username')?>
<?=$this->Form->input('password')?>
<?=$this->Form->input('RememberMe', ['checked' => false, 'type' => 'checkbox'])?>
<?=$this->Form->input(__('Login'), ['type' => 'submit'])?>
<?=$this->Form->end()?>