承接 vinkla/shield 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

vinkla/shield

最新稳定版本:9.0.0

Composer 安装命令:

composer require vinkla/shield

包简介

A HTTP basic auth middleware for Laravel

README 文档

README

This package used to be an HTTP basic auth middleware for Laravel. However, it is now deprecated as I no longer use it personally. Laravel already has built-in basic auth support for their web guard.

If you need simpler basic auth for your API, instead of depending on a third-party library, you can add it yourself. Please follow the guide below.

To begin, update your .env file with the following details:

BASIC_AUTH_USER=your-username
BASIC_AUTH_PASSWORD=your-password

Modify your config/auth.php file as follows:

'basic' => [
    'user' => env('BASIC_AUTH_USER'),
    'password' => env('BASIC_AUTH_PASSWORD'),
],

Finally, create a middleware that resembles the code below:

<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class BasicAuthMiddleware
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = config('auth.basic.user');
        $password = config('auth.basic.password');

        if (
            $request->getUser() !== $user ||
            $request->getPassword() !== $password
        ) {
            throw new UnauthorizedHttpException('Basic');
        }

        return $next($request);
    }
}

That's it! You can now use the middleware in your routes.

use App\Http\Middleware\BasicAuthMiddleware;
use App\Models\User;

Route::get('api/users', function () {
    return User::all();
})->middleware(BasicAuthMiddleware::class);

统计信息

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

GitHub 信息

  • Stars: 227
  • Watchers: 4
  • Forks: 25
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-26