PHP code example of littler / think-jwt

1. Go to this page and download the library: Download littler/think-jwt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

littler / think-jwt example snippets




return [
    'stores' => [
        // 单应用时 默认使用此配置
        'default' => [
            'sso' => [
                'enable' => true,
            ],
            'token' => [
                'signer_key'    => 'littler',
                'public_key'    => 'file://path/public.key',
                'private_key'   => 'file://path/private.key',
                'not_before'    => 0,
                'expires_at'    => 3600,
                'refresh_ttL'   => 7200,
                'signer'       => 'HS256',
                'type'         => 'Header',
                'expires_code'      => 904010,
                'refresh_code'      => 904011,
                'iss'          => 'client.littler',
                'aud'          => 'server.littler',
                'automatic_renewal' => false,
            ],
            'user' => [
                'bind' => true,
                'class'  => null,
            ]
        ],
        // 多应用时 对应应用的配置
        'admin' => [
            'sso' => [
                'enable' => false,
            ],
            'token' => [
                'signer_key'    => 'littler',
                'not_before'    => 0,
                'expires_at'    => 3600,
                'refresh_ttL'   => 7200,
                'signer'       => 'HS256',
                'type'         => 'Header',
                'expires_code'      => 904010,
                'refresh_code'      => 904011,
                'iss'          => 'client.littler',
                'aud'          => 'server.littler',
                'automatic_renewal' => false,
            ],
            'user' => [
                'bind' => false,
                'class'  => null,
            ]
        ]
    ],
    'manager' => [
        // 缓存前缀
        'prefix' => 'jwt',
        // 黑名单缓存名
        'blacklist' => 'blacklist',
        // 白名单缓存名
        'whitelist' => 'whitelist'
    ]
];


namespace app\api\controller\Auth;

use littler\jwt\Facade\Jwt;

public function login()
{
    //...登录判断逻辑

    // 自动获取当前应用下的jwt配置
    return json([
        'token' => Jwt::token($uid, ['params1' => 1, 'params2' => 2])->toString(),
    ]);

    // 自定义用户模型
    return json([
        'token' => Jwt::token($uid, ['model' => CustomMember::class])->toString(),
    ]);
}

use littler\jwt\Facade\Jwt;
use littler\jwt\Exception\HasLoggedException;
use littler\jwt\Exception\TokenAlreadyEexpired;

class User {

    public function test()
    {
        if (true === Jwt::verify($token)) {
            // 验证成功
        }

        // 验证成功
        // 如配置用户模型文件 可获取当前用户信息
        dump(Jwt::user());
    }
}


use littler\jwt\Middleware\Jwt;

// 自动获取当前应用配置
Route::get('/hello', 'index/index')->middleware(Jwt::class);

// 自定义应用 使用api应用配置
Route::get('/hello', 'index/index')->middleware(Jwt::class, 'api');

# config/jwt.php



return [

    'stores' => [
        'admin' => [
            'token' => [
                // ...其它配置
                'type' => 'Header',
                // 'type' => 'Cookie',
                // 'type' => 'Param',
                // 支持多种方式获取
                // 'type' => 'Header|Param',
            ]
        ]
    ]

];


$store = 'api';

app('jwt.manager')->destroyStoreWhitelist($store);


$store = 'api';
$uid = '9520';

app('jwt.manager')->destroyToken($id, $store);