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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?