PHP code example of oh86 / laravel-gw-auth

1. Go to this page and download the library: Download oh86/laravel-gw-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oh86 / laravel-gw-auth example snippets


return [
    'default' => env('GW_AUTH_DEFAULT_GATEWAY', 'default'),

    // 可配置后台接口网关、前台接口网关、openapi接口网关等
    'gateways' => [
        'default' => [
            'private-request' => [
                'app' => env('GW_AUTH_PRIVATE_APP'),
                'ticket' => env('GW_AUTH_PRIVATE_TICKET'),
                'ignore-check' => env('APP_DEBUG', false),  // 是否忽略校验,缺省是false
            ],
        ],

    ],
];

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Oh86\GW\Auth\Middleware\CheckPrivateRequest;

Route::post('api/private/test', [TestController::class, 'test'])->middleware([
    CheckPrivateRequest::class,
]);

return [
    // ...
    'guards' => [
        // ...

        'gw-auth' => [
            'driver' => 'gw',
            'header' => 'GW-Auth-Info',
            'user-class' => Oh86\GW\Auth\Guard\User::class,
        ]
    ],

    // ...
];

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use Oh86\GW\Auth\Middleware\CheckPrivateRequest;

Route::post('api/private/auth/test', function(Request $request) {
    $user = Auth::user();

    return $user;
})->middleware([
    CheckPrivateRequest::class,
    'auth:gw-auth',
]);

use Oh86\GW\Auth\HttpClient\PrivateRequest;

$req = new PrivateRequest([
    'baseUrl' => 'http://127.0.0.1:8000', 
    'app' => 'app1', 
    'ticket' => '...',
    ]);

$response = $req->get('api/private/test', ['foo' => 'bar']);

$status = $response->status();
$arr = $response->json();

return [
    'default' => env('GW_AUTH_DEFAULT_GATEWAY', 'default'),

    'gateways' => [
        'default' => [
            // ...

            // 服务发现配置
            'service-discovery' => [
                'private-request' => [
                    'baseUrl' => env('GW_AUTH_SERVICE_DISCOVERY_BASE_URL'),
                    'app' => env('GW_AUTH_SERVICE_DISCOVERY_APP'),
                    'ticket' => env('GW_AUTH_SERVICE_DISCOVERY_TICKET'),
                ],
            ],
        ],

    ],
];

use Oh86\GW\Auth\HttpClient\PrivateRequestWithServiceDiscovery;

$req = new PrivateRequestWithServiceDiscovery('app1');

$response = $req->get('api/private/test', ['foo' => 'bar']);

$status = $response->status();
$arr = $response->json();
shell
php artisan vendor:publish --provider='Oh86\GW\Auth\GatewayAuthServiceProvider'