Codeigniter Rest服务器摘要或基本身份验证登录

时间:2013-12-16 13:13:25

标签: php codeigniter rest

我正在使用Phil Sturgeon的REST服务器。我无法设置基本或摘要式身份验证。当调用方法时,我得到提示用户名和密码的对话框,即使我用正确的用户名和密码写它也不会接受它。我该如何解决这个问题?是我的网络主机不允许吗?

这是我在rest.php中设置auth的方式: 其余的是默认的。

$config['rest_auth']         = 'Basic';
$config['auth_source']       = '';
$config['rest_valid_logins'] = array('admin' => 'password');

3 个答案:

答案 0 :(得分:9)

我自己创建了解决方案。我必须在我的.htaccess文件中添加它。

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]  

答案 1 :(得分:6)

很抱歉写这么老的帖子。认为它可以节省一些人的时间。

请注意,此解决方案仅适用于基本身份验证。我没有用digest auth测试这个。

为了让.htaccess工作,我需要调整@ Tan的答案。

我从[L]移除了RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L],并在RewriteEngine on

之后放置了重写规则

.htaccess文件:

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

#Other rules follow

接下来,我需要更改application / config目录中的rest.php配置文件。我所做的更改如下:

$config['auth_source'] = 'ldap'$config['auth_source'] = 'library'$config['auth_library_class'] = ''$config['auth_library_class'] = 'api_auth' $config['auth_library_function'] = ''$config['auth_library_function'] = 'login'

然后,我创建了一个名为Api_auth.php的新库文件,其中包含以下代码:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Api_auth
{
    public function login($username, $password) {
        if($username == 'admin' && $password == '1234')
        {
            return true;            
        }
        else
        {
            return false;           
        }           
    }
}

希望有一天能帮到某人。

答案 2 :(得分:4)

这对我来说不起作用,但确实如此

对我来说它将运行php的服务器作为fastCGI,而不是php5_module,就像你的localhost一样。 (找到这个运行phpinfo - 服务器API:CGI / FastCGI)

这是解决方案 http://ellislab.com/forums/viewthread/173823/#825912

的.htaccess

RewriteEngine on
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L] 

libraries / REST_Controller.php

// most other servers
elseif ($this->input->server('HTTP_AUTHENTICATION'))
{
if (strpos(strtolower($this->input->server('HTTP_AUTHENTICATION')),'basic') === 0)
{
    list($username,$password) = explode(':',base64_decode(substr($this->input-  >server('HTTP_AUTHORIZATION'), 6)));
}
} 

// most other servers
elseif ( $this->input->server('HTTP_AUTHENTICATION') || $this->input-    >server('REDIRECT_REMOTE_USER') )
{
$HTTP_SERVER_AUTH = ($this->input->server('HTTP_AUTHENTICATION')) ? $this->input->server('HTTP_AUTHENTICATION') : $this->input->server('REDIRECT_REMOTE_USER'); 

if (strpos(strtolower($HTTP_SERVER_AUTH),'basic') === 0)
{
    list($username,$password) = explode(':',base64_decode(substr($HTTP_SERVER_AUTH, 6)));
}
} 
相关问题