038.CI4框架CodeIgniter,使用Jwt生成token

01、在composer.json中增加一行调用jwt的代码:

{
    "name": "codeigniter4/appstarter",
    "description": "CodeIgniter4 starter app",
    "license": "MIT",
    "type": "project",
    "homepage": "https://codeigniter.com",
    "support": {
        "forum": "https://forum.codeigniter.com/",
        "source": "https://github.com/codeigniter4/CodeIgniter4",
        "slack": "https://codeigniterchat.slack.com"
    },
    "require": {
        "php": "^8.1",
        "codeigniter4/framework": "^4.5.3",
        "firebase/php-jwt": "^5.2"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9",
        "mikey179/vfsstream": "^1.6",
        "phpunit/phpunit": "^10.5.16"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "exclude-from-classmap": [
            "**/Database/Migrations/**"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\Support\\": "tests/_support"
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "scripts": {
        "test": "phpunit"
    }
}

02、在目录中输入composer update,就可以更新到最新的了。

 03、Tx_jwt.php代码如下:

<?php

namespace App\Libraries;

use \Firebase\JWT\JWT;
use \Firebase\JWT\Key;

class Tx_jwt
{

    protected $key = '123abc';

    //http://localhost/phmci4/public/index.php/myjwt/generatejwt
    function encodeJWT($user)
    {
        $payload = array(
            'data' => $user,
            'iss' => 'TIANPAN',//签发者 可以为空
            'aud' => 'TIANPAN',//面象的用户,可以为空
            'iat' => time(),//签发时间
            'nbf' => time(), //在什么时候jwt开始生效  (这里表示生成10秒后才生效)
            'exp' => time() + 3600, //token 过期时间
        );
        $jwt_encoded = JWT::encode($payload, $this->key);
        return $jwt_encoded;
    }

    //http://localhost/phmci4/public/index.php/myjwt/decodejwt
    function decodeJWT($token)
    {
        // 此时$decoded是一个对象,你可以根据需要访问其属性
        try {
            $jwt_decoded = JWT::decode($token, new Key($this->key, 'HS256'));
            return $jwt_decoded;
        } catch (\Exception $e) {
            return $e;
        }
    }

}

04、Api001.test代码如下;

<?php namespace App\Controllers\Api;

use App\Controllers\BaseController;
use App\Libraries\Tx_jwt;


//访问地址:http://localhost/phmci4/public/index.php/api/api001/
class Api001 extends BaseController
{
    protected $Jwt;

    public function __construct()
    {
        $this->Jwt = new Tx_jwt;
    }

    //http://localhost/phmci4/public/index.php/api/api001/encodetoken
    public function encodetoken(): string
    {
        $user = array(
            'username' => 'tianpan',
            'role' => 'guest'
        );
        $token = $this->Jwt->encodeJWT($user);
        return $token;
    }

    //http://localhost/phmci4/public/index.php/api/api001/decodetoken
    public function decodetoken(): string
    {
        $token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoidGlhbnBhbiIsInJvbGUiOiJndWVzdCJ9LCJpc3MiOiJUSUFOUEFOIiwiYXVkIjoiVElBTlBBTiIsImlhdCI6MTcyNDk5NzczNCwibmJmIjoxNzI0OTk3NzM0LCJleHAiOjE3MjQ5OTc3NTR9.1Yr_4khWZYKa3IGXCJ4i0fpDCPr7RTyc21cNPclzR_k';
        $user = $this->Jwt->decodeJWT($token);
        return Json_encode($user);
    }
    //--------------------------------------------------------------------
}

05、结构目录如下:

 06、浏览器如下:

 

posted @ 2024-08-31 01:07  像一棵海草海草海草  阅读(28)  评论(0编辑  收藏  举报