PHP code example of uniondrug / token-auth-middleware

1. Go to this page and download the library: Download uniondrug/token-auth-middleware 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/ */

    

uniondrug / token-auth-middleware example snippets


return [
    'default' => [
        ......
        'providers'           => [
            ......
            \Uniondrug\TokenAuthMiddleware\TokenAuthServiceProvider::class,
        ],
    ],
];

return [
    'default' => [
        // 应用定义的中间件
        'middlewares' => [
            ...
            // 注册名为token的中间件
            'token' => \Uniondrug\TokenAuthMiddleware\TokenAuthMiddleware::class,
        ],

        // 将token中间件放在全局中间列表中
        'global'      => [
            'token', 'cors', 'cache', 'favicon', 'trace',
        ],

        ...

        // Token中间件的参数设置
        'token'       => [
            // 白名单,这个列表内的地址不需要认证,通常放登录接口等地址
            'whitelist' => [
                '/show'
            ],
            'ttl' => 7 * 86400, // 有效期7天,连续7天不登录将失效
        ],
    ],
];