This commit is contained in:
ct
2025-07-16 16:03:24 +08:00
parent 6c8a69173e
commit 1935497dd7
3 changed files with 58 additions and 3 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class HorizonBasicAuthMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle($request, Closure $next)
{
// Skip auth check if not in production environment
if (! App::environment('production')) {
return $next($request);
}
$authenticationHasPassed = false;
if ($request->header('PHP_AUTH_USER', null) && $request->header('PHP_AUTH_PW', null)) {
$username = $request->header('PHP_AUTH_USER');
$password = $request->header('PHP_AUTH_PW');
if ($username === config('horizon.basic_auth.username') && $password === config('horizon.basic_auth.password')) {
$authenticationHasPassed = true;
}
}
if ($authenticationHasPassed === false) {
return response()->make('Invalid credentials.', 401, ['WWW-Authenticate' => 'Basic']);
}
return $next($request);
}
}

View File

@@ -28,7 +28,7 @@
|
*/
'path' => env('HORIZON_PATH', 'horizon'),
'path' => env('HORIZON_PATH', 'chorizo'),
/*
|--------------------------------------------------------------------------
@@ -70,7 +70,22 @@
|
*/
'middleware' => ['web'],
'middleware' => ['web', \App\Http\Middleware\HorizonBasicAuthMiddleware::class],
/*
|--------------------------------------------------------------------------
| Horizon Basic Auth
|--------------------------------------------------------------------------
|
| These credentials will be used to authenticate access to the Horizon
| dashboard. You can set these values in your environment file.
|
*/
'basic_auth' => [
'username' => env('BASIC_AUTH_USERNAME', 'admin'),
'password' => env('BASIC_AUTH_PASSWORD', 'password'),
],
/*
|--------------------------------------------------------------------------

File diff suppressed because one or more lines are too long