CakePHP v2.6和BotDetect Captcha

时间:2015-05-04 20:26:44

标签: cakephp captcha

我在CakePHP 2.6应用程序中使用BotDetect Captcha,并按照此页面上的说明实现了它:

How To Add BotDetect Protection To CakePHP 2.6 Applications

Captcha在我需要它的控制器/视图上工作得很好。

然而,它似乎在某种程度上干扰了同一控制器使用的标准登录过程。

这是我加载BotDetect组件的控制器的标题:

$('#graph-table td').click()

这是我的登录功能:

$('#graph-table td').click(function() {
    if ($(this).hasClass("g-selected"))) {
        //Remove selection
    } else {
        //Add selection
    } 
}

这是我的AppController.php:

public $components = array('RequestHandler','Epd','BotDetect.Captcha' => array(
                'CaptchaId' => 'EpdCaptcha',
                'UserInputId' => 'CaptchaCode'));

现在,当我登录应用程序时,auth组件不会授权登录,而且它只是弹回到登录屏幕。但是当我删除BotDetect组件时,登录工作完美。我已经尝试更改加载组件的顺序,看看是否有任何区别......但无济于事。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这是一个在cakephp 2.6中集成BotDetect Captcha组件的例子,它对我来说还不错。

Controller:UsersController.php:

<?php
App::uses('AppController', 'Controller');

class UsersController extends AppController {

    public $components = array(
        'RequestHandler',
        'BotDetect.Captcha' => array(
            'CaptchaId' => 'EpdCaptcha',
            'UserInputId' => 'CaptchaCode'
        )
    );

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('logout');
        $this->Security->validatePost = false;
    }

    public function selectorg() {
        echo 'selectorg';
        $this->autoRender = false;
    }

    public function login() {

        $this->set('captchaHtml', $this->Captcha->Html());

        if ($this->request->is('post')) {

            $isHuman = $this->Captcha->Validate($this->request->data['User']['CaptchaCode']);

            unset($this->request->data['User']['CaptchaCode']);

            if ($isHuman && $this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                if (!$isHuman) {
                    $this->Session->setFlash(__('CAPTCHA validation failed, try again.'));
                } else {
                    $this->Session->setFlash(__('Invalid username or password, try again'));
                }
            }
        }

    }

    public function logout() {
        return $this->redirect($this->Auth->logout());
    }

}

Controller:AppController.php:

class AppController extends Controller {

    public $components = array(
        'Security',
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'users',
                'action' => 'selectorg'
            ),
            'logoutRedirect' => array(
                'controller' => 'users',
                'action' => 'login'
            ),
            'authenticate' => array('Form' => array('passwordHasher' => 'Blowfish'))
        )
    );

}

查看:login.ctp

<?php
    echo $this->Html->css(CaptchaUrls::LayoutStylesheetUrl(), array('inline' => false));

    echo $this->Form->create('User');

    echo $this->Form->input('username');
    echo $this->Form->input('password');

    echo $this->Html->div('captcha', $captchaHtml, false);

    // Captcha code user input textbox
    echo $this->Form->input('CaptchaCode', array(
            'label' => 'Retype the characters from the picture:',
            'maxlength' => '10',
            'style' => 'width: 300px;'
        )
    );

    echo $this->Form->end('Submit');
?>

型号:User.php

<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {
    public $name = 'User';

    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please enter your username'
            ),
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'Username already exists'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please enter your password'
            )
        )
    );

    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $passwordHasher = new BlowfishPasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }
        return true;
    }
}