您可以创建一个扩展原始 AuthComponent 的新组件。然后您覆盖登录方法并在那里添加您的其他身份验证。这个解决方案非常方便,而且不会太侵入,即使 AuthComponent 不是为了这样扩展而设计的。
App::import('Core', 'Auth');
class AuthenticationComponent extends AuthComponent {
public $components = array('Session', 'RequestHandler', 'Sso');
public $loginRedirect = array('controller' => 'frontpage');
public $Controller = null;
public $loginAction = array('controller' => 'login');
public $authorize = 'actions';
public $loginError = Notifications::LOGIN_FAILED;
public $authError = Notifications::ACTION_NOT_ALLOWED;
public function login($data) {
if(parent::login($data)) {
// OTHER AUTHENTICATION HERE
return true;
}
else {
// stuff
}
return false;
}
}