jwt 的下载无需复制博客和博客园

1:浏览器输入jwt 官网

官方地址

https://jwt.io/introduction

 

 

 2:复制composer 命令

 

 

 

 

 composer require firebase/php-jwt

3:文件下载目录

(1)laravel 下载的文件位置

 

 (2):tp文件位置

 

 

4:文件新建一个service 目录,新建一个TokenService,进行创建方法名称

<?php


namespace App\service;



class TokenService
{

    /**
     *
     * 创建token
     */
    public function createToken(){

    }

    /**
     *
     * 验证token
     */
    public function  checkToken(){

    }
}

5:参看MD README.md 进行复制 修改为以下的代码,创建token的加入用户id的形参。

 

 

 

 

<?php


namespace App\service;

// 第一步引入token
use Firebase\JWT\JWT;
use Firebase\JWT\Key;


class TokenService
{

    /**
     *
     * 创建token
     */

public function createToken($userId){ $key = "example_key"; $payload = array( "iss" => "http://example.org", "aud" => "http://example.com", "iat" => 1356999524, "nbf" => 1357000000, "user_id"=>$userId ); /** * IMPORTANT: * You must specify supported algorithms for your application. See * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40 * for a list of spec-compliant algorithms. */ $token = JWT::encode($payload, $key, 'HS256'); // $decoded = JWT::decode($jwt, new Key($key, 'HS256')); return $token; } /** * * 验证token */ public function checkToken($token){ $key = "example_key"; $payload = array( "iss" => "http://example.org", "aud" => "http://example.com", "iat" => 1356999524, "nbf" => 1357000000 ); /** * IMPORTANT: * You must specify supported algorithms for your application. See * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40 * for a list of spec-compliant algorithms. */ // $token = JWT::encode($payload, $key, 'HS256'); $decoded = JWT::decode($token, new Key($key, 'HS256')); return $decoded; } }

 补充:复制过来还的进行修改;签发的对象可以是空的

 

 

6:控制器 根据用户id进行调用即可

 

posted @ 2022-02-21 16:59  王越666  阅读(735)  评论(0编辑  收藏  举报