试图在Laravel 9.3上建立一个带有请求数据标识的多租户



我试图在Laravel上设置一个带有请求数据标识的多租户,但我找不到任何信息。

按照这个问题开始就这么简单吗https://tenancyforlaravel.com/docs/v3/quickstart

然后按照这个步骤进行?https://tenancyforlaravel.com/docs/v3/tenant-identification/#Request-数据标识:~:text=public%20static%20property(。-,请求%20数据%20识别,-您%20权利%20想要

所以从这个改变我的帐篷路线

<?php
declare(strict_types=1);
use IlluminateSupportFacadesRoute;
use StanclTenancyMiddlewareInitializeTenancyByDomain;
use StanclTenancyMiddlewarePreventAccessFromCentralDomains;
/*
|--------------------------------------------------------------------------
| 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 IlluminateSupportFacadesRoute;
use StanclTenancyMiddlewareInitializeTenancyByRequestData;
use StanclTenancyMiddlewarePreventAccessFromCentralDomains;
/*
|--------------------------------------------------------------------------
| 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');
});
});

接下来应该做的是创建一个中间件,在该中间件中验证头中的x-tender或文档中建议的查询参数。

我附上了一个例子来处理标题,JWT应该是这样的:

/**
* Handle an incoming request.
*
* @param  IlluminateHttpRequest  $request
* @param  Closure(IlluminateHttpRequest): (IlluminateHttpResponse|IlluminateHttpRedirectResponse)  $next
* @return IlluminateHttpResponse|IlluminateHttpRedirectResponse
*/
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);
}
}

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

最新更新