dtv7174 2017-02-21 09:23
浏览 360
已采纳

“laldvel中的jwt中无法从请求中解析令牌”

I am facing "The token could not be parsed from the request" error in JWT in Laravel.

I have tried same code in localhost(Xampp in windows 7) it is working but on server it is not working.

I have passed "Authorization" token in header and also changed .htaccess file.

Please check below screenshot for passed token in request in angular 2.

enter image description here

Below is .htaccess file. (I have also tried this code in root .htaccess file and public .htaccess file both)

<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>

And I have used below code for authenticated token.

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();
    }
}
  • 写回答

1条回答 默认 最新

  • duanbi8089 2017-02-21 10:54
    关注

    Because of the problematic exclusion of the Authorization header by Apache, then if you need to save time you can pass the token into your requests, then in your controller, check if you have the token with:

    public function image_upload(Request $request){
        return $request->token;
    .....
    }
    

    Did you have any result? if you don't then you have to check if your request parameter has the token field. If you have the token in this controller, then you can still use the parseToken() method or simply (maybe for debugging purpose) use:

    public function image_upload(Request $request){
        $user = JWTAuth::toUser($request->token));
        .......
    }
    

    I think this should be sufficient, the only mistake might be that you have applied a middleware that is causing this issue to the route you are accessing.

    PS: I tried this on Laravel 5.2*

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码