0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

laravel stancl/tenancyのリクエストデータを使ったドメイン識別に苦戦したこと

Posted at

確認方法

curlで確認できる

curl -H "X-Tenant: 自分のテナントの名前" http://localhost(自分のアクセス先に合わせる)

詰まったところ

これを追加するとHOSTヘッダーとcentral_domainsを比較してしまうので、一生api.phpしか使えない。

// routes/web.php, api.php or any other central route files you have

foreach (config('tenancy.central_domains') as $domain) {
    Route::domain($domain)->group(function () {
        // your actual routes
    });
}

PreventAccessFromCentralDomains::class,これもHOSTヘッダーとcentral_domainsを比較してしまうので、一生ここの中に書いてあるルーティングは404になる。(嘘かも)

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');
    });
});

venderの中にlog追加して見てみた。
getHostの方はリクエストを受けた側(laravel側)のドメインが出力された。
config側はもちろんconfigに書いたドメインが出てきていた。
だからPreventAccessFromCentralDomains::class,はapiにするときは使えない。

class PreventAccessFromCentralDomains
{
    /**
     * Set this property if you want to customize the on-fail behavior.
     *
     * @var callable|null
     */
    public static $abortRequest;

    public function handle(Request $request, Closure $next)
    {
        /******************これを追加********************/
        Log::error('PreventAccessFromCentralDomains Middleware: Host => ' . $request->getHost());
        Log::error('PreventAccessFromCentralDomains Middleware: Central Domains => ' . implode(', ', config('tenancy.central_domains')));
        /******************これを追加********************/

        if (in_array($request->getHost(), config('tenancy.central_domains'))) {
            $abortRequest = static::$abortRequest ?? function () {
                abort(404);
            };

            return $abortRequest($request, $next);
        }
        

        return $next($request);
    }
}

これも追加するとHOSTヘッダーとcentral_domainsを比較してしまうので、一生api.phpしか使えない。
なので追加しない方がいい。

// RouteServiceProvider

protected function mapWebRoutes()
{
    foreach ($this->centralDomains() as $domain) {
        Route::middleware('web')
            ->domain($domain)
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }
}

protected function mapApiRoutes()
{
    foreach ($this->centralDomains() as $domain) {
        Route::prefix('api')
            ->domain($domain)
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }
}

protected function centralDomains(): array
{
    return config('tenancy.central_domains', []);
}

既存でコメントアウトしてあったこれを解除してnamespace追加して使ってあげたらいい感じ。

            // Route::middleware('api')
            //     ->prefix('api')
            //     ->group(base_path('routes/api.php'));

CORSのせいでアクセスできなかった。
下記エラーはnginxとlaravelのcors.phpでallow_originの記述が重複していたせいで起こっていた。
どちらか1つにするといい。

 Access to XMLHttpRequest at '自分のドメイン(laravel側)' from origin '自分のドメイン(フロント側)' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '自分のドメイン(フロント側), 自分のドメイン(フロント側)', but only one is allowed.

下記エラーは許可されていないパスに向けてapiリクエストしていた時に発生した。
laravelのcors.phpでpathの設定を見直すといい。

自分のドメイン(フロント側)/:1 Access to XMLHttpRequest at '自分のドメイン(laravel側)' from origin '自分のドメイン(フロント側)' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?