Php CodeIgniter REST API基本身份验证不工作

Php CodeIgniter REST API基本身份验证不工作,php,rest,codeigniter,Php,Rest,Codeigniter,我正在尝试在我的站点上集成REST服务。我正在使用这个例子。到目前为止,我能够成功调用所需的数据。但是,当我想向它添加授权时,当我试图从邮递员处访问它时,会出现以下错误: { "status": false, "error": "Unauthorized" } 如果我删除了身份验证,它会再次工作。有人知道这是一个bug还是我遗漏了什么吗??这是我的配置: ------------------------------------------------------------------

我正在尝试在我的站点上集成REST服务。我正在使用这个例子。到目前为止,我能够成功调用所需的数据。但是,当我想向它添加授权时,当我试图从邮递员处访问它时,会出现以下错误:

{
  "status": false,
  "error": "Unauthorized"
}
如果我删除了身份验证,它会再次工作。有人知道这是一个bug还是我遗漏了什么吗??这是我的配置:

-------------------------------------------------------------------------
| REST Login
|--------------------------------------------------------------------------
|
| Set to specify the REST API requires to be logged in
|
| FALSE     No login required
| 'basic'   Unsecured login
| 'digest'  More secured login
| 'session' Check for a PHP session variable. See 'auth_source' to set the
|           authorization key
|
*/
$config['rest_auth'] = 'basic';

|--------------------------------------------------------------------------
| REST Login Usernames
|--------------------------------------------------------------------------
|
| Array of usernames and passwords for login, if ldap is configured this is ignored
|
*/
$config['rest_valid_logins'] = ['admin' => '1234'];

我也无法通过url访问它。即使我输入了凭据,身份验证弹出窗口仍会持续出现。请帮助

它正在检查PHP服务器变量中的用户名和密码

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

$password = $this->input->server('PHP_AUTH_PW');
application/libraries/REST\u Controller.php中
Line:1971

protected function _prepare_basic_auth()
    {
        // If whitelist is enabled it has the first chance to kick them out
        if ($this->config->item('rest_ip_whitelist_enabled'))
        {
            $this->_check_whitelist_auth();
        }
        // Returns NULL if the SERVER variables PHP_AUTH_USER and HTTP_AUTHENTICATION don't exist
        $username = $this->input->server('PHP_AUTH_USER');
        $http_auth = $this->input->server('HTTP_AUTHENTICATION');
        $password = NULL;
        if ($username !== NULL)
        {
            $password = $this->input->server('PHP_AUTH_PW');
        }
        elseif ($http_auth !== NULL)
        {
            // If the authentication header is set as basic, then extract the username and password from
            // HTTP_AUTHORIZATION e.g. my_username:my_password. This is passed in the .htaccess file
            if (strpos(strtolower($http_auth), 'basic') === 0)
            {
                // Search online for HTTP_AUTHORIZATION workaround to explain what this is doing
                list($username, $password) = explode(':', base64_decode(substr($this->input->server('HTTP_AUTHORIZATION'), 6)));
            }
        }
        // Check if the user is logged into the system
        if ($this->_check_login($username, $password) === FALSE)
        {
            $this->_force_login();
        }
    }

您可以将
PHP\u AUTH\u USER
PHP\u AUTH\u PW Server
变量配置为在API配置中配置的,以验证和解决此错误,或者您可以使用API密钥作为替代验证方法。

我设法找到了问题所在!原来我的服务器api是fastcgi,所以解决方案是更改REST\u控制器文件。我发现这个链接适用于快速cgi用户


希望这对别人有帮助

我很难做到这一点,通过在我的.htaccess中添加以下内容(在index.php位之后),我成功地实现了这一点:


在您的情况下,我怀疑标题不符合要求,这将确保它们符合要求。

您是否检查了“登录用户名和密码数组,如果配置了ldap,则忽略此项”?我在设置ldap时已将其删除!谢谢回复!不幸的是,即使添加了此项功能,它仍然不起作用。同样的错误也存在。您可以将服务器变量设置为
$\u server['PHP\u AUTH\u USER']='admin'并通过
echo$\u服务器['PHP\u AUTH\u USER']进行验证密码类似。您还可以将
$config['rest\u auth']=false如果不想使用此基本身份验证,可以使用API密钥进行身份验证。
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]