lengbin/hyperf-jwt

dev-master 2020-08-19 07:37 UTC

This package is auto-updated.

Last update: 2024-04-19 16:01:19 UTC


README

68747470733a2f2f6879706572662e6f73732d636e2d68616e677a686f752e616c6979756e63732e636f6d2f6879706572662e706e67

Hyperf Jwt


If You Like This Please Give Me Star

Install

The preferred way to install this extension is through composer.

Either run

composer require lengbin/hyperf-jwt

or add

"lengbin/hyperf-jwt": "*"

to the require section of your composer.json file.

Request 详情

"lengbin/jwt": "dev-master"

Configs

    /config/autoload/jwt.php
    
    return [
        'alg' => 'HMACSHA256',
        'key' => 'ice',
        // jwt 过期时间
        'exp' => 2 * 3600,
        // 刷新token 过期时间
        'ttl' => 24 * 3600 * 30,
        // 是否 单点登录
        'sso' => false
    ];

Publish

      
php ./bin/hyperf.php vendor:publish lengbin/hyperf-jwt

Usage

<?php

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Lengbin\Jwt\JwtInterface;

/**
 * Class IndexController
 * @package App\Controller
 * @Controller()
 */
class IndexController extends AbstractController
{

    /**
     * @Inject()
     * @var JwtInterface
     */
    public $jwt;

    /**
     * @RequestMapping(path="/", methods={"get", "post"})
     *
     * @return array
     */
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();
        // 生成token
        $token = $this->jwt->makeToken([
             'method'  => $method,
             'message' => "Hello {$user}.",
         ]);
        // 生成 刷新token
        // $this->jwt->generate($exp);
        // 生成 刷新token
        // $this->jwt->generateRefreshToken();
        // 验证 获得自定义参数
        // $this->jwt->verifyToken($token);
        //注销
        // $this->jwt->logout();
        return [
            'method'  => $method,
            'message' => "Hello {$user}.",
            'token'   => $token,
        ];
    }

}