2016-08-30 7 views
3

ユーザー認証のためにミドルウェアで次のコードをJWT Authに追加しました。これはミドルウェアが処理するすべてのルートで正常に動作します。Laravel - JWT Authトークンを要求から解析できませんでした

public function handle($request, Closure $next) 
{ 
    if ($request->has('token')) { 
     try { 
      $this->auth = JWTAuth::parseToken()->authenticate(); 
      return $next($request); 
     } catch (JWTException $e) { 
      return redirect()->guest('user/login'); 
     } 
    } 
} 

しかし、トークンが適切に渡さなっているが、それでも私はJWTExceptionを取得しています郵便法との1つのルートの - 私は

をしようとしたときにトークンが同じルート上に、要求から解析できませんでした
public function handle($request, Closure $next) 
{ 
    if ($request->has('token')) { 
     try { 
      dd($request->input('token')); 
      $this->auth = JWTAuth::parseToken()->authenticate(); 
      return $next($request); 
     } catch (JWTException $e) { 
      return redirect()->guest('user/login'); 
     } 
    } 
} 

出力:私は有効なトークンWHを見ることができています

"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9iaXNzIjoiaHR0cDpcL1wvbG9jYWxob3N0OjgwMDFcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNDcyNTI4NDU0LCJleHAiOjE0NzI1MzIwNTQsIm5iZiI6MTQ3MjUyODQ1NCwianRpIjoiM2E0M2ExYTZlNmM5NjUxZDgxYjZhNDcxMzkxODJlYjAifQ.CH8ES2ADTCrVWeIO8uU31bGDnH7h-ZVTWxrdXraLw8s" 

私は別のルートにアクセスするために使用していますが、他のすべてのルートでも問題なく動作しています。

ありがとうございます!!!

答えて

3

あなたの説明から、私はJWT Authのソースファイルを確認しました。 - クラスタイモン\ JWTAuth \ JWTAuthライン191で

219、二つの機能があります。

/** 
* Parse the token from the request. 
* 
* @param string $query 
* 
* @return JWTAuth 
*/ 
public function parseToken($method = 'bearer', $header = 'authorization', $query = 'token') 
{ 
    if (! $token = $this->parseAuthHeader($header, $method)) { 
     if (! $token = $this->request->query($query, false)) { 
      throw new JWTException('The token could not be parsed from the request', 400); 
     } 
    } 

    return $this->setToken($token); 
} 

/** 
* Parse token from the authorization header. 
* 
* @param string $header 
* @param string $method 
* 
* @return false|string 
*/ 
protected function parseAuthHeader($header = 'authorization', $method = 'bearer') 
{ 
    $header = $this->request->headers->get($header); 

    if (! starts_with(strtolower($header), $method)) { 
     return false; 
    } 

    return trim(str_ireplace($method, '', $header)); 
} 

チェックそれらのロジックを、私はあなたのリクエストヘッダが適切に提供されていないと信じています。

http POST http://${host}/api/v1/product/favorite/111 "Authorization: Bearer ${token}"

if (! $token = $this->parseAuthHeader($header, $method)) { // all your get method not passed this step 
    if (! $token = $this->request->query($query, false)) { // all your post method stucked here 
     throw new JWTException('The token could not be parsed from the request', 400); 
    } 
} 

proprlyヘッダは次のようです。

上記は私があなたに提供できるものです。あなたの考えを助けてくれることを願っています。そうでなければ、それらの2つの関数からデバッグすることができます。

+0

ご協力ありがとうございます。私が提供したソリューションをお試しいただきます。ありがとうございました。 –

+0

@ akshaykhale、これがあなたを助けてくれることを願っています。私たちはどちらも評判を得ることができます –

+0

あなたの答えは非常に役に立ちました.JWTAuthファイルは要求からトークンを読み取ることができませんでした。手伝ってくれてありがとう。 –

関連する問題