我使用Code igniter 4和JWT创建了restful API。Login API运行良好,并生成了auth令牌。但我无法使用令牌获取登录详细信息,在尝试获取授权令牌时,它显示了一个错误(空值)。
public function details(){
$key = $this->getKey();
$authHeader = $this->request->getHeader("Authorization"); //return null
$authHeader = $authHeader->getValue(); //line 149 error, caused $authHeader is null
$token = $authHeader;
// $token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJUaGVfY2xhaW0iLCJhdWQiOiJUaGVfQXVkIiwiaWF0IjoxNjQxNTQ0MTQzLCJuYmYiOjE2NDE1NDQxNTMsImV4cCI6MTY0MTU0Nzc0MywiZGF0YSI6eyJpZCI6IjkiLCJhY2NvdW50X2lkIjoiY2ljY2NjIiwibmFtZSI6ImNvZGUgaWduaXRlciJ9fQ.TI3zztWxIYZxoa_vhTB04YoGMaq4GdD4bxzmrt8QAH0";
try{
$decoded = JWT::decode($token,$key,array("HS256"));
if($decoded){
$response = [
'status' => 200,
'error' => false,
'message' => 'Account details',
'data' => [
'account' => $decoded
]
];
return $this->respondCreated($response);
}
}catch(Exception $ex){
$response = [
'status' => 401,
'error' => true,
'message' =>'Access denied',
'data' => []
];
return $this->respondCreated($response);
}
}
Postman 调查结果
{
"title": "Error",
"type": "Error",
"code": 500,
"message": "Call to a member function getValue() on null",
"file": "/var/www/html/project-root/app/Controllers/Account.php",
"line": 149,
"trace": [
{
"file": "/var/www/html/project-root/vendor/codeigniter4/framework/system/CodeIgniter.php",
"line": 825,
"function": "details",
"class": "App\\Controllers\\Account",
"type": "->",
"args": []
},
{
"file": "/var/www/html/project-root/vendor/codeigniter4/framework/system/CodeIgniter.php",
"line": 412,
"function": "runController",
"class": "CodeIgniter\\CodeIgniter",
"type": "->",
"args": [
{}
]
},
{
"file": "/var/www/html/project-root/vendor/codeigniter4/framework/system/CodeIgniter.php",
"line": 320,
"function": "handleRequest",
"class": "CodeIgniter\\CodeIgniter",
"type": "->",
"args": [
null,
{
"handler": "file",
"backupHandler": "dummy",
"storePath": "/var/www/html/project-root/writable/cache/",
"cacheQueryString": false,
"prefix": "",
"ttl": 60,
"reservedCharacters": "{}()/\\@:",
"file": {
"storePath": "/var/www/html/project-root/writable/cache/",
"mode": 416
},
"memcached": {
"host": "127.0.0.1",
"port": 11211,
"weight": 1,
"raw": false
},
"redis": {
"host": "127.0.0.1",
"password": null,
"port": 6379,
"timeout": 0,
"database": 0
},
"validHandlers": {
"dummy": "CodeIgniter\\Cache\\Handlers\\DummyHandler",
"file": "CodeIgniter\\Cache\\Handlers\\FileHandler",
"memcached": "CodeIgniter\\Cache\\Handlers\\MemcachedHandler",
"predis": "CodeIgniter\\Cache\\Handlers\\PredisHandler",
"redis": "CodeIgniter\\Cache\\Handlers\\RedisHandler",
"wincache": "CodeIgniter\\Cache\\Handlers\\WincacheHandler"
}
},
false
]
},
{
"file": "/var/www/html/project-root/public/index.php",
"line": 35,
"function": "run",
"class": "CodeIgniter\\CodeIgniter",
"type": "->",
"args": []
}
]
}
Postman 屏幕截图
如果我硬编码令牌,我可以得到登录细节。为什么这行$authHeader = $this->request->getHeader("Authorization");
返回空?.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
2条答案
按热度按时间bfrts1fy1#
下面是我用来获取头的授权令牌的方法。
在public/.htaccess文件中,我有如下配置:
在控制器中,apache_request_headers() Package 器用于获取头部。
不过,这只适用于Apache服务器。
0mkxixxg2#
将其添加到
.htaccess
文件然后得到这样的标题
更新
我注意到您没有使用
Authorization
选项卡,而是手动设置标题。如果您手动设置授权标题,请确保值字段以Bearer
开头,如或者只需使用“授权”选项卡并选择
Bearer Token