PHP code example of yzh52521 / webman-jwt-auth

1. Go to this page and download the library: Download yzh52521/webman-jwt-auth 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/ */

    

yzh52521 / webman-jwt-auth example snippets




return [
      'manager' => [
        //是否开启黑名单,单点登录和多点登录的注销、刷新使原token失效,必须要开启黑名单
        'blacklist_enabled'      => true,
        //黑名单缓存的前缀
        'blacklist_prefix'       => 'yzh52521',
        //黑名单的宽限时间 单位为:秒,注意:如果使用单点登录,该宽限时间无效
        'blacklist_grace_period' => 0,
      ],
       'stores' => [
        // 单应用
        'default' => [
            'login_type'    => 'mpo', 
            'signer_key'    => 'oP0qmqzHS4Vvml5a',
            'public_key'    => 'file://path/public.key',
            'private_key'   => 'file://path/private.key',
            'expires_at'    => 3600,
            'refresh_ttL'   => 7200,
            'leeway'        => 0,
            'signer'        =>'HS256',
            'type'          => 'Header',
            'auto_refresh'  => false,
            'iss'           => 'webman.client.api',
            'event_handler' => Event::class,
            'user_model'    => User::class
        ],
        // 多应用
        'admin'   => [
            'login_type'    => 'mpo', 
            'signer_key'    => 'oP0qmqzHS4Vvml5a',
            'public_key'    => 'file://path/public.key',
            'private_key'   => 'file://path/private.key',
            'expires_at'    => 3600,
            'refresh_ttL'   => 7200,
            'leeway'        => 0,
            'signer'        => 'HS256',
            'type'          => 'Header',
            'auto_refresh'  => false,
            'iss'           => 'webman.client.admin',
            'event_handler' => Event::class,
            'user_model'    => User::class
        ],
    ]
];


public function login()
{
    //...登录判断逻辑
       $config = JwtAuth::getConfig();
    // 自动获取当前应用下的jwt配置
    return json([
        'token' => JwtAuth::token($uid, ['params1' => 1, 'params2' => 2])->toString(),
        'token_type' => $config->getType(),
        'expires_in' => $config->getExpires(),
        'refresh_in' => $config->getRefreshTTL(),
    ]);
    
    // 自定义用户模型
    return json([
        'token' => JwtAuth::token($uid, ['user_model' => CustomMember::class])->toString(),
        'token_type' => $config->getType(),
        'expires_in' => $config->getExpires(),
        'refresh_in' => $config->getRefreshTTL(),
    ]);
}

    实例化 参数应用名  
    $JwtAuth =new yzh52521\JwtAuth\JwtAuth('default'); 
    
    //生成token
    $config = $JwtAuth->getConfig();
    // 自动获取当前应用下的jwt配置
    return json([
        'token' => $JwtAuth->token($uid, ['params1' => 1, 'params2' => 2])->toString(),
        'token_type' => $config->getType(),
        'expires_in' => $config->getExpires(),
        'refresh_in' => $config->getRefreshTTL(),
    ]);
    
      //验证token
       $JwtAuth =new yzh52521\JwtAuth\JwtAuth('default'); 
       try {
           $data= $JwtAuth->verify($token);
           dump($data);
        } catch (JwtException $e) {
            throw new TokenInvalidException('登录校验已失效, 请重新登录', 401);
        }
        //解析token
        $JwtAuth =new yzh52521\JwtAuth\JwtAuth('default'); 
        
        $JwtAuth->parseToken($token);
        $JwtAuth->getVerifyToken();