This article brings the content is about PHP custom token class and will generate tokens placed in the HTTP request Header authorization code, there is a certain reference value, the need for friends can refer to, I hope to help you.
Today, when using Laravel to write something, you need to interact with the front end, look at the JWT (JSON Web token), and then try to write a simple token class that stores the member ID and member permissions in token, and when interacting with the front end, Place the generated tokens in the authorization of the HTTP request header, organized as follows:
<?phpnamespace App\http\controllers\auth;use Illuminate\http\request;use App\http\controllers\controller;class Tokencontroller extends controller{/** * Header * @var array */private static $header = [" Type "=" "token", "alg" = "HS256"]; /** * Create payload * @param $memberId * @param $permission * @return Array */private static Func tion Payload ($memberId, $permission) {return ["ISS" = "http://api.creatshare.com" , "iat" = $_server[' request_time ', "exp" = $_server[' Request_ti ME '] + 7200, "GivenName" = "Creatshare", "MemberID" and "= $memberId, "Permission" = $permission]; }/** * Encode data * @param $data * @return String */private static function encode ($data) { Return Base64_encode (Json_encode($data)); }/** * Generate a signature * @param $header * @param $payload * @param string $secret * @return S Tring */private static function signature ($header, $payload, $secret = ' secret ') {return Hash_hmac (' Sha $header. $payload, $secret); }/** * Generate a token * @param $memberId * @param $permission * @return String */public stat IC function Createtoken ($memberId, $permission) {$header = Self::encode (self:: $header); $payload = Self::encode (self::p ayload ($memberId, $permission)); $signature = Self::signature ($header, $payload); Return $header. '. '. $payload. '.' . $signature; }/** * Check a token * @param $JWT * @param string $key * @return array|string */public static function Checktoken ($JWT, $key = ' secret ') {$token = explode ('. ', $JWT); if (count ($token)! = 3) return 'Token invalid '; List ($header, $payload, $sign) = $token; if (Self::signature ($header, $payload)!== $sign) return ' token invalid '; $header = Json_decode (Base64_decode ($header), Json_object_as_array); $payload = Json_decode (Base64_decode ($payload), Json_object_as_array); if ($header [' type ']! = ' token ' | | $header [' ALG ']! = ' HS256 ') return ' token invalid '; if ($payload [' ISS ']! = ' http://api.creatshare.com ' | | $payload [' GivenName ']! = ' creatshare ') return ' t Oken invalid '; if (Isset ($payload [' exp ']) && $payload [' exp '] < time ()) return ' timeout '; return [' MemberID ' = $payload [' MemberID '], ' permission ' and ' = ' $payload [' permis Sion ']; }/** * Get a token * @return NULL */public static functionGetToken () {$token = null; if (Isset ($_server[' http_authorization ')) $token = $_server[' http_authorization ']; return $token; }}
$token = Token::createtoken ($member _id, $member _permission);//Create a token$ token = Token::gettoken (); Get Token$result = Token::checktoken () from the HTTP request header; Parse token