【发布时间】:2017-02-21 09:23:14
【问题描述】:
我在 Laravel 的 JWT 中遇到“无法从请求中解析令牌”错误。
我在 localhost(Windows 7 中的 Xampp)中尝试了相同的代码,但在服务器上它不工作。
我已经在标头中传递了 "Authorization" 令牌,并且还更改了 .htaccess 文件。
请检查下面的屏幕截图以获取角度 2 中请求中传递的令牌。
下面是 .htaccess 文件。 (我也在根 .htaccess 文件和公共 .htaccess 文件中尝试过这段代码)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</IfModule>
我已经使用下面的代码来验证令牌。
public function image_upload(Request $request){
try{
if($user = JWTAuth::parseToken()->authenticate()){
\Api::success(['data' => $user]);
}else{
\Api::error("User is not found.");
}
}
catch(\Tymon\JWTAuth\Exceptions\TokenExpiredException $e){
\Api::error(['type'=> 'expired','message' => 'Your login has been expired. Please login and try again.']);
exit();
}
catch(\Tymon\JWTAuth\Exceptions\TokenInvalidException $e){
\Api::error(['type'=> 'invalid','message' => $e->getMessage()]);
exit();
}
catch(\Tymon\JWTAuth\Exceptions\JWTException $e){
\Api::error(['type'=> 'invalid','message' => $e->getMessage()]);
exit();
}
}
【问题讨论】:
-
能不能回
$request->bearerToken()看看能不能得到? -
此外,这里说明的一个问题可能与您的情况有关? github.com/tymondesigns/jwt-auth/issues/786 即
# Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] -
我试过 $request->bearerToken() 它返回 NULL 值
-
我已经更改了 .htaccess 文件,但是我遇到了同样的错误。
-
如果您需要节省时间来解决这个问题,您可以简单地将令牌作为参数传递到您的请求中,即
http://somehost.come/alien/?token=theTokenHere