yidas/yii2-access-router 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

yidas/yii2-access-router

Composer 安装命令:

composer require yidas/yii2-access-router

包简介

Yii 2 user authentication & authorization router

README 文档

README

   

Yii 2 Access Router


Yii 2 user authentication & authorization router

Latest Stable Version Latest Unstable Version License

FEATURES

  • Yii 2 User Authentication/Authorization for route level Integration

  • RESTful API Authentication by Access Token support

  • HTTP Request Login by Access Token support

Access Router is a simple user access filtered on route level which supports authentication and authorization. Different from Yii2 Access Control Filter (ACF), this User Authorization can specify routes but not only in controller-actions level.

OUTLINE

REQUIREMENTS

This library requires the following:

  • PHP 5.4.0+
  • Yii 2.0.0+

INSTALLATION

Install via Composer in your Yii2 project:

composer require yidas/yii2-access-router

CONFIGURATION

Setup a Access Router component and then add it into bootstrap for your application configuration:

return [
    'bootstrap' => ['log', 'access'],
    'components' => [
        'access' => [
            'class' => 'yidas\filters\AccessRouter',
            'except' => ['site/login', 'site/register'],
            'denyCallback' => function() {
                return Yii::$app->response->redirect(['/site/login']);
            },
        ],
        // ...
    ],
    // ...
];
  1. Create a component called access which uses yidas\filters\AccessRouter as class with configuration.

  2. Add this access component into bootstrap list.

Options

Key Type Default Description
except array ['*'] Excepted routes for identity verification check. ['{controller}/{action}', '{d}/{c}/{a}']
denyCallback callable null DenyCallback for HTTP authentication
httpAuth array HTTP authentication framework feature
httpLogin array HTTP request method login feature
exceptErrorAction boolean true Error action would be excepted through filter while turning on

USAGE

Except

Access Router implements Access Control Filter (ACF) for routes that the user is must in login status to pass through the filter from any routes except specified ones.

You can setup excepted routes that skip the user authorization. The except setting with [*] value means that the user authorization is disabled:

'access' => [
    'class' => 'yidas\filters\AccessRouter',
    'except' => ['site/login'], //`site/login` is the login page which can not bypass user authorization
],

HTTP Authentication

Access Router supports automatically authenticating client's request by HTTP Authentication with bearer schemes (RFC 6750), you can enable it by setting up httpAuth configuration:

'access' => [
    'class' => 'yidas\filters\AccessRouter',
    'except' => ['site/login', 'site/register'],
    'httpAuth' => [
        'enable' => true,
        'denyCallback' => function() {
            $response = Yii::$app->response;
            $response->statusCode = 401;
            $response->format = \yii\web\Response::FORMAT_JSON;
            $response->data = ['message' => 'Access Denied'];
            return $response->send();
        },
    ],
],

HTTP Authentication login will disable session for one time access uasge, which equals to \Yii::$app->user->enableSession = false;

Options

Key Type Default Description
enable boolean false Enable HTTP authentication
denyCallback callable null DenyCallback for HTTP authentication
forced boolean true Force to authorize by HTTP authentication
key string 'AUTHORIZATION' The header key

Request Method Login

Access Router also supports automatically login client's request by HTTP GET/POST parameter by giving access token, you can enable it by setting up httpLogin configuration:

'access' => [
    'class' => 'yidas\filters\AccessRouter',
    'except' => ['site/login', 'site/register'],
    'httpLogin' => [
        'enable' => true,
        'method' => 'post'
        'only' => ['site/login'],
        // 'key' => 'access_token',
    ],
],

For above configuration, you could login by accessing route site/login with correct access_token body value (Content-Type: application/x-www-form-urlencoded).

Request Method Login is same as form login that the session is enable, and the duration time could be customized.

For GET method, If you setup 'method' => 'get' with 'only' => ['*'], then you can login by any routes with correct access_token parameter. For example: //example.com/?access_token={valid-user-access-token}

For security reasons, it's not recommended to use GET method that passes access token in parameter.

Options

Key Type Default Description
enable boolean false Enable HTTP request method login
method string 'post' Parameter's Methods of get/post
only array ['*'] Allowed routes for login. ['{controller}/{action}', '{d}/{c}/{a}']
duration integer 3600 * 24 * 30 Seconds of login duration
key string 'access_token' Parameter's key
forced boolean true Force to authorize by HTTP authentication

POST Method without CSRF

If you uses post method and want to disable global CSRF validatiob, you can set enableCsrfValidation to false for request configuration:

'components' => [
    'request' => [
        'csrfParam' => '_csrf-backend',
        'enableCsrfValidation' => false,
    ],

If you just want to disable CSRF for some controllers/actions, dynamically setting enableCsrfValidation for controller.

ADDITIONS

ACF for Global

If you want to use original Yii 2 Access Control Filter (ACF) for global route instead of Access Router's User Authorization, just comment out the except of Access Router and add ACF rules into 'as beforeRequest' in config:

'bootstrap' => ['log', 'access'],
'components' => [
    'access' => [
        'class' => 'yidas\filters\AccessRouter',
        'except' => ['*'], // Equal to comment out
    ],
    // ...
],
'as beforeRequest' => [
    'class' => 'yii\filters\AccessControl',
    'rules' => [
        [
            'allow' => true,
            'actions' => ['login'],
        ],
        [
            'allow' => true,
            'roles' => ['@'],
        ],
    ],
    'denyCallback' => function () {
        return Yii::$app->response->redirect(['site/login']);
    },
],

Warning: ACF could only defines actions but not routes, which the actions could be applied by every controllers.

For above setting example, login excepted action could be matched by any controller such as site/login, controller/login.

REFERENCE

Yii 2 - Application Structure > Application Events

RFC7617 - The 'Basic' HTTP Authentication Scheme

yidas/yii2-access-router 适用场景与选型建议

yidas/yii2-access-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 103 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「authorization」 「Authentication」 「access」 「yii2」 「access-router」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yidas/yii2-access-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yidas/yii2-access-router 我们能提供哪些服务?
定制开发 / 二次开发

基于 yidas/yii2-access-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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