【发布时间】:2018-07-11 19:23:59
【问题描述】:
我正在使用 codeigniter、Rest Server 和 firebase php-jwt 创建 rest api。以下是我创建 jwt 令牌的代码。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . '/libraries/REST_Controller.php';
require_once APPPATH . '/libraries/JWT.php';
use \Firebase\JWT\JWT;
class Welcome extends REST_Controller
{
public function __construct($config = 'rest')
{
parent::__construct($config);
$this->load->model("Users_model");
}
public function login_post()
{
$username = $this->post('username');
$password = $this->post('password');
$invalidLogin = ['invalid' => $username];
if(!$username || !$password){
$this->response($invalidLogin, REST_Controller::HTTP_NOT_FOUND);
}
$id = $this->Users_model->checkLogin($username,$password);
if($id) {
$token['id'] = $id;
$token['username'] = $username;
$date = new DateTime();
$token['iat'] = $date->getTimestamp();
$token['exp'] = $date->getTimestamp() + 60*60*5;
$output['id_token'] = JWT::encode($token, $this->config->item('jwt_secret_key'));
$this->set_response($output, REST_Controller::HTTP_OK);
} else {
$this->set_response($invalidLogin, REST_Controller::HTTP_NOT_FOUND);
}
}
/******** This is target test function having error **********/
public function test_post()
{
$token = $this->input->get_request_header('Authorization');
try{
$payload = JWT::decode($token, $this->config->item('jwt_secret_key'),array('HS256'));
$this->response($payload, REST_Controller::HTTP_OK);
} catch (Exception $ex) {
$response = array("message" => $ex->getMessage());
$this->response($response, REST_Controller::HTTP_UNAUTHORIZED);
}
}
}
我的库文件夹中有以下文件
- BeforeValidException.php
- ExpiredException.php
- Format.php
- JWT.php
- REST_Controller.php
- SignatureInvalidException.php
当我使用带有错误令牌(更改签名中的字符串)的邮递员进行测试时,给出的错误是
遇到未捕获的异常
Type: Error Message: Class 'Firebase\JWT\SignatureInvalidException' not found Filename: .../application/libraries/JWT.php</p> Line Number: 113
Jwt.php 上有以下代码
// Check the signature
if (!static::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) {
throw new SignatureInvalidException('Signature verification failed'); // This is line 113
}
但我的库文件夹中有这个文件。 任何帮助将不胜感激。
【问题讨论】:
-
你能把JWT文件放在这里吗
-
我已经给出了相应仓库的链接。我将更新 q 并发布该文件中的代码
-
我认为你必须在 JWT.php 之前 require_once 签名
-
@Jinesh 这解决了我的问题,谢谢!!!
-
我推荐这个库与 php 的 firebase 一起使用,它是最好的:link
标签: php rest codeigniter jwt