尝试在Laravel 9.3上设置带有请求数据标识的多租户

70gysomp  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(185)

我试图在Laravel上设置一个带有请求数据标识的多租户,但我找不到任何相关信息。
遵循这个快速启动https://tenancyforlaravel.com/docs/v3/quickstart就这么简单吗
然后按照以下步骤操作?https://tenancyforlaravel.com/docs/v3/tenant-identification/#请求数据标识:~:text=public%20static%20property).-,请求数据标识,-您可能想要
所以改变我的帐篷路线

<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;

/*
|--------------------------------------------------------------------------
| Tenant Routes
|--------------------------------------------------------------------------
|
| Here you can register the tenant routes for your application.
| These routes are loaded by the TenantRouteServiceProvider.
|
| Feel free to customize them however you want. Good luck!
|
*/

Route::middleware([
    'web',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    Route::get('/', function () {
        return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
    });
});

对此:

<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;

/*
|--------------------------------------------------------------------------
| Tenant Routes
|--------------------------------------------------------------------------
|
| Here you can register the tenant routes for your application.
| These routes are loaded by the TenantRouteServiceProvider.
|
| Feel free to customize them however you want. Good luck!
|
*/

Route::middleware([
    'web',
    InitializeTenancyByRequestData::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    Route::get('/', function () {
        return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
    });
});
ubof19bj

ubof19bj1#

接下来应该做的是创建一个中间件,在其中验证头部中的x-tenant或文档中建议的查询参数。
我附上了一个例子来处理头文件,JWT应该是这样的:

/**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if($user = JWTAuth::parseToken()->authenticate())
        {
            if ($user->global_id != $request->header('x-tenant'))
            {
                return response()->json(['errors' => 'You do not have access to this tenant'], 401);
            }

            return $next($request);
        }
    }

当然,您必须根据应用的性质考虑其他安全方面。

相关问题