定制 ramosisw/cakephp-jwt-claims 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ramosisw/cakephp-jwt-claims

Composer 安装命令:

composer require ramosisw/cakephp-jwt-claims

包简介

CakePHP plugin for read claims on jwt token

README 文档

README

Build Status Coverage Total Downloads License

CakePHP 3.6+ Component to read claims of JWT on Authorization

Installation

composer require ramosisw/cakephp-jwt-claims

Configuration:

Setup ClaimsComponent:

    // In your controller, for e.g. src/Controller/AppController.php
    public function initialize()
    {
        parent::initialize();
        //Config JWT with ADmad Plugin for more info see https://github.com/ADmad/cakephp-jwt-auth
        $this->loadComponent('Auth', [/*..*/]);
        //Load Claims component
        $this->loadComponent('RamosISW/Jwt.Claims',[
            'claims_key' => 'data', //name where is claims on JWT Payload
            //Wath claims be read
            'data' => [
                'user_id', 'user_email', 'user_name'
            ]
        ]);
    }

Working

To read claims on route that user can access

    public function index(){
        //read user email sends on token
        $this->log($this->Claims->user_email);
        
        //set claims to use on view
        $this->set('Claims', $this->Claims);
    }

Token Generation

You can use \Firebase\JWT\JWT::encode() of the firebase/php-jwt lib, which this plugin depends on, to generate tokens.

The payload should have the "sub" (subject) claim whos value is used to query the Users model and find record matching the "id" field.

Generate Claims

    public function generateToken($user){
        $token = \Firebase\JWT\JWT::encode([
                'sub' => $user['id'],
                'exp' => time() + 604800,
                'data' => [
                    'user_id' => $user['id'],
                    'user_email' => $user['email'],
                    'user_name' => $user['username']
                ]
        ], \Cake\Utility\Security::getSalt());
        
        return $token;
    }

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固