2017-01-15 27 views
1

私は今何時間も探していますが、この問題の解決策を見つけることができません。PHP JWTトークンが無効な署名

これはJWTトークンを生成するためのコードです。私はhttps://github.com/firebase/php-jwtライブラリを使用しました。

 $tokenId = base64_encode(mcrypt_create_iv(32)); 
     $issuedAt = time(); 
     $notBefore = $issuedAt + 10;    //Adding 10 seconds 
     $expire  = $notBefore + 60;   // Adding 60 seconds 
     $serverName = 'serverName'; // Retrieve the server name from config file 

     $secretKey = base64_decode(getenv('JWT_SECRET')); 

     $data = [ 
      'iat' => $issuedAt,   // Issued at: time when the token was generated 
      'jti' => $tokenId,   // Json Token Id: an unique identifier for the token 
      'iss' => $serverName,  // Issuer 
      'nbf' => $notBefore,  // Not before 
      'exp' => $expire,   // Expire 
      'data' => [     // Data related to the signer user 
       'userId' => '1', // userid from the users table 
       'userName' => $UserName, // User name 
      ] 
     ]; 

     $jwt = JWT::encode(
       $data,  //Data to be encoded in the JWT 
       $secretKey, // The signing key 
       'HS256'  // Algorithm used to sign the token 
     ); 

     $unencodedArray = ['jwt' => $jwt]; 
     echo json_encode($unencodedArray); 

そして私はhttps://jwt.io/

enter image description here

でトークンを検証する誰もがこの問題で私を助けることができますか?私は現在JWTで新しくなっています。 Btw、私のプロジェクトはSlim APIです。

ありがとうございました。

+0

'$ notBefore = $ issuedAt + 10;'を '$ notBefore = $ issuedAt + 0;'に変更することをお勧めします! –

答えて

4

正しい秘密鍵をhttps://jwt.io/に渡していないために署名検証が失敗する$secretKeyの値をPHPコードから渡す必要があります。スクリーンショットによると、文字列secretを渡しています。

+0

ありがとうございます。私はhttps://jwt.io/で「秘密」のキーワードを変更する必要があることに気付かなかった – Joseph