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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站