【发布时间】:2014-04-03 12:15:01
【问题描述】:
我想向resource/index.json 发出请求,但由于我的index 未经身份验证是不允许的,它会将我重定向到登录页面。当没有发送用户名:密码时,这就是我想要的行为
问题是我如何将AuthComponent 设置为与Form 和Basic 一起使用,并且仅在请求通过api 前缀时检查基本。
另外,当在标题中找到用户名和密码时它会自动进行身份验证还是我必须手动进行?
【问题讨论】:
我想向resource/index.json 发出请求,但由于我的index 未经身份验证是不允许的,它会将我重定向到登录页面。当没有发送用户名:密码时,这就是我想要的行为
问题是我如何将AuthComponent 设置为与Form 和Basic 一起使用,并且仅在请求通过api 前缀时检查基本。
另外,当在标题中找到用户名和密码时它会自动进行身份验证还是我必须手动进行?
【问题讨论】:
在各自的控制器中添加几行
class NameController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow("index");
}
}
这将允许索引没有身份验证。
【讨论】:
我决定使用 Friend's of Cake TokenAuthenticate,是的,它可以与 FormAuthenticate 一起使用,所以我可以同时使用两者。
事实上,它会根据是否存在_token 参数或X-MyApiTokenHeader 标头自动选择要使用的组件。
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form',
'Authenticate.Token' => array(
'parameter' => '_token',
'header' => 'X-MyApiTokenHeader',
'userModel' => 'User',
'scope' => array('User.active' => 1),
'fields' => array(
'username' => 'username',
'password' => 'password',
'token' => 'public_key',
),
'continue' => true
)
)
)
);
【讨论】: