【问题标题】:CakePHP basic auth on API (json) requestCakePHP 对 API (json) 请求的基本身份验证
【发布时间】:2014-04-03 12:15:01
【问题描述】:

我想向resource/index.json 发出请求,但由于我的index 未经身份验证是不允许的,它会将我重定向到登录页面。当没有发送用户名:密码时,这就是我想要的行为

问题是我如何将AuthComponent 设置为与FormBasic 一起使用,并且仅在请求通过api 前缀时检查基本。

另外,当在标题中找到用户名和密码时它会自动进行身份验证还是我必须手动进行?

【问题讨论】:

    标签: json cakephp


    【解决方案1】:

    在各自的控制器中添加几行

    class NameController extends AppController {
        public function beforeFilter() {
                parent::beforeFilter();
                $this->Auth->allow("index");
            }
    }
    

    这将允许索引没有身份验证。

    【讨论】:

    • @ChristopherFrancisco 可能是我无法理解您的情况。
    • 我的问题是“当在标题中找到用户名和密码时它会自动进行身份验证还是我必须手动进行?”
    【解决方案2】:

    我决定使用 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
                )
            )
        )
    );
    

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多