Skip to content

ice-leng/hyperf-jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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,
        ];
    }

}

Releases

No releases published

Packages

No packages published

Languages