CakePHP 2.0中是否可以按请求进行无状态HTTP Basic / Digest身份验证?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了CakePHP 2.0中是否可以按请求进行无状态HTTP Basic / Digest身份验证?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
该手册尚不清楚如何实现这一点(它假设你已经知道你正在做什么,在某些情况下感觉就像是事后的想法),而且我一直在试图解决问题时为了公平而摸不着头脑.

问题:通过HTTP身份验证标头对所有API请求进行身份验证

至于我已经能够测试,我可以在CakePHP中使用Basic auth和普通的基于表单的登录,但只能首先点击我在Auth组件中定义的登录操作.当我直接访问该站点时,这很好,并按预期工作(Digest除外,它似乎完全错误).但是,通过cURL,除非我已经登录,否则我没有运气.

显然,对于API来说,这远非理想.在做我想做的事情之前,我不想向/ login发布请求,我不能指望用户手动登录,因此Cake有一个cookie可供阅读.它需要是无国籍的.

任何尝试提供身份验证凭据以及我发出的每个请求(通过cURL)都会被忽略,我收到403错误.无论是登录方法还是任何Auth类都没有被触及.

我需要做些什么才能使Cake的行为像实际API一样,并允许我根据请求进行无状态授权?我是否必须推出自己的解决方案?

解决方法

我有一个集中的API,允许通过HTTP摘要进行用户身份验证,并要求用户登录许多用户相关的功能. CakePHP强制登录的方式是检查操作是否需要登录,重定向到您的登录操作(认为/ users / LOGin),然后您可以重定向.

我通过执行以下操作创建了我的API:

//Config/routes.PHP
///////////////////////////
/**
 * Users Controller routes for REST API 
 */
    Router::maPResources('users');
/**
 * Parses extensions for data serialization 
 */
    Router::parseExtensions();

//Controller/UserController.PHP
////////////////////////////////
<?PHP

App::uses('DigestAuthenticate','Controller/component/Auth/');

class UsersController extends AppController {

    VAR $name = 'Users';

    //Login callback
    function login() {
        //dont render for login,just a call back for auth
        $this->autoRender = false;

        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        }
    }

    //GET /users.[XMl|json]
    //this is the basic call that @R_512_2187@s user authentication
    //basically a login then echo
    function index() {
        if ($this->Auth->login()) {
            $user = $this->Auth->user();

            $this->User->id = $user['id'];
            $this->User->saveField('last_login',date('Y-m-d H:i:s'));

            $this->set('response',array(
                'response' => array(
                    'code' => 'users_auth_success','message' => 'User has passed authentication','data' => $user
                )
            ));
            //will serialize to xML or JSON based on extension
            $this->set('_serialize','response');
        } 
    }
}

?>

然后,您可以在以下内容中使用此API:

$c = curl_init($uri . '.' . $this->_format);

curl_setopt($c,CURLOPT_RETURNtransfer,1);
curl_setopt($c,CURLOPT_USERPWD,$login['user'] . ':' . $login['pass']);
curl_setopt($c,CURLOPT_HTTPAUTH,CURLAUTH_DIGEST);
curl_setopt($c,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($c,CURLOPT_FOLLOWLOCATION,true);

$response = curl_exec($c);
$info = curl_getinfo($c);

curl_close($c);

if($info['http_code'] == $this->_http_codes['OK']) {
    //success
    if($this->_format == 'xml')
        $response = Xml::toArray(Xml::build($response));
    else//JSON
        $response = json_decode($response);
        return $response['response']['data'];
} else if($info['http_code'] == $this->_http_codes['Unauthorized']) {
    return false;
} else {
    return null;
}

脚本宝典总结

以上是脚本宝典为你收集整理的CakePHP 2.0中是否可以按请求进行无状态HTTP Basic / Digest身份验证?全部内容,希望文章能够帮你解决CakePHP 2.0中是否可以按请求进行无状态HTTP Basic / Digest身份验证?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。