Giter VIP home page Giter VIP logo

url-sign's Introduction

简单URL签名

关于

url-sign 包是一个简单的URL参数签名验证,主要在Laravel中使用。

安装和卸载

添加 wenhsing/url-sign 到你的 composer.json 文件并更新对应的依赖:

composer require wenhsing/url-sign

如果你不想使用或者遇到了其他问题,可以通过下面的方式卸载,然后再进行一次安装:

composer remove wenhsing/url-sign

基础使用方式

require_once "./vendor/autoload.php";

$c = new \Wenhsing\UrlSign\UrlSignManager();

$c->verify('https://wenhsing.com/?timestamp=1639640957&sign=14e8ae7845907b031bfd8af08594f421'));

在 Laravel 中使用

打开 app/Http/Kernel.php 文件,将 UrlSignMiddleware 中间件添加到 $middlewareGroups 的对应字段下字段下:

protected $middlewareGroups = [
    // 其他分组

    'api' => [
        \Wenhsing\UrlSign\Laravel\Middleware\UrlSignMiddleware::class,
        // 其他中间件
    ]
];

当然,你可以将 UrlSignMiddleware 中间件添加到 $middleware 属性中,在全局进行签名验证:

protected $middleware = [
    \Wenhsing\UrlSign\Laravel\Middleware\UrlSignMiddleware::class,
    // 其他中间件
];

如果你想要自定义验证失败后如何返回,可以通过继承 \Wenhsing\UrlSign\Laravel\Middleware\UrlSignMiddleware 类,然后重写 errorResponse 方法,然后将上面的添加的中间件替换成你创建的:

<?php

namespace App\Http\Middleware;

use Wenhsing\UrlSign\Laravel\Middleware\UrlSignMiddleware;

class VerifySign extends UrlSignMiddleware
{
    public function errorResponse($request, Closure $next)
    {
        //
    }
}

配置

如果你的 config 目录下不存在 url-sign.php 配置文件的话,可以通过这种方式生成文件:

php artisan vendor:publish --tag="url-sign"

然后修改 url-sign.php 配置文件即可:

<?php
return [
    'default' => env('URL_SIGN_DRIVER', 'md5'),

    'md5' => [
        // 需要排除的路径,条目可以参考如下写法
        'except'     => [
            '/',
            'a/test',
            'b/test2/*',
        ],

        // 签名加签密钥
        'secret_key' => '',

        // 允许签名请求时间与当前时间的误差值
        'time_error' => 300,
    ]
];

驱动扩展

如果想要自定义驱动,可以编写一个类,然后通过 extend 进行扩展,类似

require_once "./vendor/autoload.php";

$c = new \Wenhsing\UrlSign\UrlSignManager();

$c->extend('extendName', function ($config) {
    return new Example($config);
});

$c->driver('extendName')->verify('https://wenhsing.com/?timestamp=1639640957&sign=14e8ae7845907b031bfd8af08594f421'));

在 Laravel 扩展可以参考下面的方式:

$c = app(Wenhsing\UrlSign\UrlSignManager::class);
$c->extend('wenhsing', function ($config) {
    return new Test($config);
});
$c->driver('wenhsing')->verify('https://wenhsing.com/?timestamp=1639640957&sign=14e8ae7845907b031bfd8af08594f421');

url-sign's People

Contributors

wenhsing avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.