【问题标题】:Cakephp and custom authentification for mobile applicationCakephp 和移动应用程序的自定义身份验证
【发布时间】:2012-03-04 16:16:13
【问题描述】:
嘿,我正在开发一个与 Android 应用程序相结合的网络应用程序。现在我希望在我的移动应用程序中,用户必须使用与 Web 应用程序相同的用户数据登录。所以我想向控制器发送用户名和密码,在这个控制器的登录操作中,用户应该被验证并且额外的用户 id 应该被发送回应用程序(id 用于应用程序中的几个操作) .我寻找 CakePHP 的 Auth Component 但我没有找到任何解决我的问题的方法。我希望你能帮助我。
【问题讨论】:
标签:
android
api
cakephp
authentication
【解决方案1】:
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index','view');
$this->set('logged_in', $this->Auth->loggedIn());
$this->set('current_user',$this->Auth->user());
if($this->name == 'Specific') {
// for the specific controller
$this->Auth->authenticate = array('Basic');
} else {
// everything else
}
}
查看 KVZ 的 rest 插件,可能会感兴趣 https://github.com/kvz/cakephp-rest-plugin
【解决方案3】:
AuthComponent 有一个login() 方法,可用于手动登录用户。根据您使用的 Cake 版本,此方法还会检查您提供的凭据(实际上,Auth 在 2.0 中以一种对您这样的情况更有用的方式进行了完全重组,值得一看!)。
您可以通过任何方式(XML、POST 参数)从您的 Android 应用程序发送登录凭据,在您的登录操作中提取它们并将它们发送到 AuthComponent(或者,在 2.0 中,编写一个自定义身份验证器对象来处理全部)。