定制 kuick/security 二次开发

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

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

kuick/security

最新稳定版本:v1.3.1

Composer 安装命令:

composer require kuick/security

包简介

Kuick Security is a package for security related tasks. Includes PSR-15 middleware implementation

README 文档

README

Latest Version PHP Total Downloads GitHub Actions CI codecov Software License

Security package implementing PSR-15 middleware

Key features

  1. PSR-15(https://www.php-fig.org/psr/psr-15/) security middleware implementation
  2. Support for flexible Guards (any callable)
  3. Guardhouse service with methods to register Guards (regex path support)

Installation

composer require kuick/security

Usage

1. Create a guard

A guard is any invokable object (or closure) accepting a ServerRequestInterface and returning void|null. Throw a Kuick\Http\HttpException to deny the request.

use Kuick\Http\HttpException;
use Kuick\Http\Message\Response;
use Psr\Http\Message\ServerRequestInterface;

class BearerTokenGuard
{
    public function __invoke(ServerRequestInterface $request): void
    {
        $authHeader = $request->getHeaderLine('Authorization');
        if (!str_starts_with($authHeader, 'Bearer valid-token')) {
            throw new HttpException(Response::HTTP_UNAUTHORIZED, 'Invalid or missing token');
        }
    }
}

2. Register guards in the Guardhouse

Use addGuard(string $path, object $guard, array $methods = [...]) to register guards. The $path is a full regex (anchored as #^…$#). Named capture groups are merged into the request's query params.

By default (when $methods is omitted), a guard matches all HTTP methods: GET, POST, PUT, PATCH, DELETE, OPTIONS. HEAD is automatically included whenever GET is listed.

use Kuick\Security\Guardhouse;
use Psr\Log\NullLogger;

$guardhouse = (new Guardhouse(new NullLogger()))
    // protect all routes with a token check
    ->addGuard('/api/.*', new BearerTokenGuard())
    // restrict a specific route to GET only
    ->addGuard('/api/resource/(?P<id>\d+)', new BearerTokenGuard(), ['GET']);

3. Wire up the PSR-15 middleware

Pass the Guardhouse to SecurityMiddleware and add it to your PSR-15 middleware stack.

use Kuick\Security\SecurityMiddleware;

$middleware = new SecurityMiddleware($guardhouse, new NullLogger());

// Example with any PSR-15-compatible dispatcher (e.g. Relay, Slim, etc.)
$response = $middleware->process($serverRequest, $nextHandler);

If a guard throws a Kuick\Http\HttpException the exception propagates up — your framework's error handler is responsible for converting it into an HTTP response. If all guards pass, the request is forwarded to $nextHandler.

Path regex & captured parameters

Regex captures (named or positional) from the matched path are merged into the request's query params before the guard is invoked:

// Guard registered for: '/users/(?<userId>\d+)'
// Request: GET /users/42
// Inside the guard, $request->getQueryParams()['userId'] === '42'

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固