简体   繁体   English

PHP中的REST身份验证(CodeIgniter)

[英]REST Authentication in PHP (CodeIgniter)

I writing REST API form my web application. 我从我的Web应用程序编写REST API。 Application is written using CodeIgniter framework. 应用程序使用CodeIgniter框架编写。 Application itself is working fine, but I'm stuck on making REST Authentication. 应用程序本身工作正常,但我仍然坚持进行REST身份验证。 I think that basic Http Authentication will be good enough for some time. 我认为基本的Http身份验证在一段时间内已经足够好了。 Public API is not yet planned. 公共API尚未规划。

Is there any code example how to achieve REST Authentication so after user is authenticated he can freely call all protected methods. 是否有任何代码示例如何实现REST身份验证,因此在用户通过身份验证后,他可以自由调用所有受保护的方法。

I have written up a REST Controller to make your REST applications easier to build. 我编写了一个REST控制器,使您的REST应用程序更易于构建。 You can read all about it on NetTuts: Working with RESTful services in CodeIgniter . 您可以在NetTuts上阅读所有相关内容:在CodeIgniter中使用RESTful服务

If you use HTTPS, you can use Basic authentication and it's very easy to do. 如果您使用HTTPS,则可以使用基本身份验证,这很容易。 Just add following code to your controller, 只需将以下代码添加到控制器,

   if (empty($this->input->server('PHP_AUTH_USER')))
   {
       header('HTTP/1.0 401 Unauthorized');
       header('HTTP/1.1 401 Unauthorized');
       header('WWW-Authenticate: Basic realm="My Realm"');
       echo 'You must login to use this service'; // User sees this if hit cancel
       die();
    }

    $username = $this->input->server('PHP_AUTH_USER');
    $password = $this->input->server('PHP_AUTH_PW');

    // Check username and password

I use mod_php, your auth variable names maybe different if using other SAPI modules. 我使用mod_php,如果使用其他SAPI模块,您的auth变量名可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM