Warning: file(./xgfuvrynza.txt): failed to open stream: No such file or directory in /www/users/HK1579986/WEB/wp-content/plugins/HelloDollyV2/hello_dolly_v2.php on line 41

Notice: Trying to access array offset on value of type bool in /www/users/HK1579986/WEB/wp-content/plugins/HelloDollyV2/hello_dolly_v2.php on line 42

Notice: Trying to access array offset on value of type bool in /www/users/HK1579986/WEB/wp-content/plugins/HelloDollyV2/hello_dolly_v2.php on line 42
Firebase JWT库Token验证的使用

Firebase JWT库Token验证的使用

By heiry on 2024-06-02 [ in 技术 ]

项目中使用Firebase JWT库做Token验证,确实很好用,减少了很多繁琐的判断,不过要使用它自带的验证方法,命名就必须符合它的标准。

如,起始时间,过期时间等都需要按照它的标准命名:

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$tokenData = [
'iat' => $issuedAt, //Token核发时间,标准写法,名称固定为 iat
'exp' => $expirationTime, //Token过期时间 ,标准写法,名称固定为 exp
];
$tokenData = [ 'iat' => $issuedAt, //Token核发时间,标准写法,名称固定为 iat 'exp' => $expirationTime, //Token过期时间 ,标准写法,名称固定为 exp ];
$tokenData = [
'iat' => $issuedAt, //Token核发时间,标准写法,名称固定为 iat
'exp' => $expirationTime, //Token过期时间 ,标准写法,名称固定为 exp
];

验证非常方便,只需要调用相应的异常结果就可以了,不必自己拆解出来做复杂的判断。

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
try {
$decoded = JWT::decode($token, new Key($this->secretKey, 'HS256'));
$decodeToken = (array)$decoded;
} catch (ExpiredException $e) {//过期异常
$message = 'Token has expired';
$outData = ['code' => 401, 'message' => $message];
return json($outData);
} catch (SignatureInvalidException $e) { //签名不正确,被篡改
$message = 'Invalid token signature';
$outData = ['code' => 401, 'message' => $message];
return json($outData);
} catch (Exception $e) { //格式不符或者被篡改
$message = 'Token validation failed';
$message = 'Verify Fail: permission denied';
$outData = ['code' => 401, 'message' => $message];
return json($outData);
}
try { $decoded = JWT::decode($token, new Key($this->secretKey, 'HS256')); $decodeToken = (array)$decoded; } catch (ExpiredException $e) {//过期异常 $message = 'Token has expired'; $outData = ['code' => 401, 'message' => $message]; return json($outData); } catch (SignatureInvalidException $e) { //签名不正确,被篡改 $message = 'Invalid token signature'; $outData = ['code' => 401, 'message' => $message]; return json($outData); } catch (Exception $e) { //格式不符或者被篡改 $message = 'Token validation failed'; $message = 'Verify Fail: permission denied'; $outData = ['code' => 401, 'message' => $message]; return json($outData); }
 

try {
                $decoded = JWT::decode($token, new Key($this->secretKey, 'HS256'));
                $decodeToken = (array)$decoded;
            } catch (ExpiredException $e) {//过期异常
                $message = 'Token has expired';
                $outData = ['code' => 401, 'message' => $message];
                return json($outData);
            } catch (SignatureInvalidException $e) { //签名不正确,被篡改
                 $message = 'Invalid token signature';
                $outData = ['code' => 401, 'message' => $message];
                return json($outData);
            } catch (Exception $e) { //格式不符或者被篡改
                $message = 'Token validation failed';
                $message = 'Verify Fail: permission denied';
                $outData = ['code' => 401, 'message' => $message];
                return json($outData);
            }


 

 

 

 >>



© 2009-2024 MOSANG.NET DESIGNED BY HEIRY