【问题标题】:Request Header missing authorisation - Codeigniter rest请求标头缺少授权 - Codeigniter 休息
【发布时间】:2018-07-17 08:33:18
【问题描述】:

我正在使用 Codeigniter 休息服务器和 firebase php-jwt 创建一个休息服务。 创建了一个 api 来返回操作员列表。要访问此 api 客户端必须在标头中发送令牌。样品请求是 -

GET /index.php/operators/prepaid HTTP/1.1
主机:testing.mydomain.in
授权:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjExNTIxNCIsImlhdCI6MTUxNzk4MjU1NywiZXhwIjoxNTE4MDAwNTU3fQ.PZYh3OlSsKGo_ihPPSm7RrU5BbTNaeTN1fKlNcOZ2r4 邮递员令牌:933f3b1d-7934-d30a-11bf-f80f3912f433

控制器代码

use \Firebase\JWT\JWT;
class Operators extends REST_Controller 
{
    private $_payload;

    public function __construct($config = 'rest')
    {
        parent::__construct($config);
        $token = $this->input->get_request_header('Authorization');
        if(!$token) {
            $output = array("Error" => "Access Denied");
            $this->response($output, REST_Controller::HTTP_UNAUTHORIZED);
        }
        try {
            $this->_payload = JWT::decode($token, $this->config->item('jwt_secret_key'),array('HS256'));
        } catch (Exception $ex) {
            $output = array("Error" => $ex->getMessage());
            $this->response($output, REST_Controller::HTTP_UNAUTHORIZED);
        }
        $this->load->model('Operators_Data');
    }       

    public function prepaid_get()
    { 
        $operators = $this->Operators_Data->getOperatorsByService(1);
        $this->response($operators);
    }
}

我得到以下结果

{
     "Error": "Access Denied"
}

如果令牌不存在,则从控制器构造函数返回。但我在标头授权中发送令牌。

这在我的本地主机上运行(它返回操作员列表)。但是当我 从测试服务器尝试它总是返回“拒绝访问”。

更新:很确定服务器忽略了“授权”标头。

也尝试过

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

.htaccess 中的设置

非常感谢任何帮助。

【问题讨论】:

  • 所有标题都得到了什么? php.net/manual/en/function.getallheaders.php
  • { "User-Agent": "PostmanRuntime/7.1.1", "Host": "testing.mydomain.in", "Postman-Token": "e33c8281-15e0-46b9-88c1- 26597780e33c”,“连接”:“keep-alive”,“接受”:“/”,“接受编码”:“gzip,放气”,“缓存控制”:“无缓存” " }
  • 所以您的授权标头丢失了。您确定将其包含在您的 Postman 请求中吗,因为您似乎不是。
  • 我在标题选项卡中设置键“授权”和身份验证返回令牌作为值。不在授权选项卡中
  • 查看这篇文章是否对你有帮助。 stackoverflow.com/questions/15253957/…

标签: php rest codeigniter jwt


【解决方案1】:

添加

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

.htaccess 文件的顶部在我的情况下解决了它

【讨论】:

  • 将该代码添加到 .htaccess 后,它仍然无法正常工作。所以我通过在 下添加 httpd-xampp.conf(如果你使用 XAMPP)解决了这个问题,这对我有用。
  • @flowdee,谢谢你拯救了我的一天.....它对我有用。
猜你喜欢
  • 2014-12-16
  • 2017-12-19
  • 2020-11-12
  • 1970-01-01
  • 2012-08-12
  • 1970-01-01
  • 2012-10-27
  • 2011-05-04
相关资源
最近更新 更多