CakePHP的2.1 - 作为一个Web应用程序和REST服务与认证 [英] CakePHP 2.1 - As a web application and REST service with Authentication
问题描述
我目前正在开发其目前的形式验证的应用程序的CakePHP。我也想打开这个应用程序为其他应用程序通过REST连接。
我知道的CakePHP将能够做到这一点使用
路由器::马presources()
和
路由器:: parseExtensions()
不过,我不能确定如何得到这个工作与基本说或Digest HTTP认证。
我已经得到了AppController.php以下
公共$组件=阵列(
会话,
'验证'=>阵列(
'身份验证'=>阵列(
'形成'
)
的LoginAction =>阵列(
'管理员'= GT;假,
控制器=> '用户',
'行动'=> '登录'
)
loginRedirect'=>阵列(
控制器=> '用户',
'行动'=> '家'
)
)
);
如果该身份验证领域,我例如在基本了 - 登录到基于Web版本时,我得到一个HTTP认证盒,而不是基于web的表单
。什么是这样做的最佳方式?我能想到的此刻是创建一个单独的ApiController和手动的唯一办法做鉴定?
任何建议将真棒。
更新:
这是我的修订code这是给我正确的水煤浆 - 我pretty肯定应该有更好的方式来做到这一点。
。 类AppController的扩展控制器{ 公共$组件=阵列(
会话,
'RequestHandler',
'验证'=>阵列(
的LoginAction =>阵列(
'管理员'= GT;假,
控制器=> '用户',
'行动'=> '登录'
)
loginRedirect'=>阵列(
控制器=> '用户',
'行动'=> '家'
)
)
); 公共$佣工=阵列(HTML,形式,会话); 公共职能beforeFilter(){
$头= $ _ SERVER ['HTTP_AUTHORIZATION'];
如果($头){
$这个 - > Auth->身份验证=阵列(基本);
}
}}
公共职能beforeFilter(){ //如果使用REST更改认证
如果($这个 - > PARAMS ['分机'] =='JSON'){
$这个 - > Auth->身份验证=阵列(基本);
}}
这将检查一个JSON的扩展,如果请求包含了它 - 然后切换到基本身份验证
I'm currently developing a CakePHP application which currently has form authentication. I would also like to open up this application for other applications to connect to via REST.
I know that CakePHP would be able to do this using the
Router::mapResources()
and
Router::parseExtensions()
However, I'm unsure how to get this working with say Basic or Digest HTTP authentication.
I've got the following in the AppController.php
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form'
),
'loginAction' => array(
'admin' => false,
'controller' => 'users',
'action' => 'login'
),
'loginRedirect' => array(
'controller' => 'users',
'action' => 'home'
)
)
);
If for the authenticate field, I had in 'Basic' for example - when logging into the web based version, I get an HTTP auth box and not the web based form.
What is the best way of doing this? The only way I can think of at the moment is to create a separate ApiController and manually do authentication?
Any advise would be awesome.
Update:
This is my revised code which is giving me the correct behavour - I'm pretty sure that there should be a better way to do this.
class AppController extends Controller {
public $components = array(
'Session',
'RequestHandler',
'Auth' => array(
'loginAction' => array(
'admin' => false,
'controller' => 'users',
'action' => 'login'
),
'loginRedirect' => array(
'controller' => 'users',
'action' => 'home'
)
)
);
public $helpers = array('Html', 'Form', 'Session');
public function beforeFilter() {
$header = $_SERVER['HTTP_AUTHORIZATION'];
if($header) {
$this->Auth->authenticate = array('Basic');
}
}
}
public function beforeFilter() {
// Change the authentication if using REST
if($this->params['ext'] == 'json') {
$this->Auth->authenticate = array('Basic');
}
}
This checks for a JSON extension, if the request contains it - then switch to Basic authentication.
这篇关于CakePHP的2.1 - 作为一个Web应用程序和REST服务与认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!