0

我有一个 Laravel 应用程序,可以从多个客户端访问,所有这些客户端都有自己的域(例如 myclient.com)。此外,还有一个核心客户端(在本示例中使用域 coreclient.org)。

与核心客户端关联的用户能够为任何其他客户端注册用户。

任何其他客户的管理员用户只能为其客户注册用户。

这使用了 Fortify 的基本代码。

当核心客户端为不同的客户端创建新用户时,直到涉及帐户验证电子邮件,这一切都很好。验证电子邮件中的链接使用核心客户端域,而不是新用户所属的客户端。

例如http://coreclient.org/email/verify/1/dd8...8ad?expires=1639608547&signature=718...1ee

它需要是http://myclient.com/email/verify/1/dd8...8ad?expires=1639608547&signature=718...1ee

为了解决这个问题,我在生成后替换了 url 字符串中的域,但这会导致签名无效,因为我假设这是根据创建新用户的人的域生成的。

到目前为止,这是我在 AuthServiceProvider 文件的引导功能中所拥有的……有什么想法吗?

VerifyEmail::createUrlUsing(function ($notifiable) {

    $verifyUrl = URL::temporarySignedRoute(
        'verification.verify',
        Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
        [
            'id' => $notifiable->getKey(),
            'hash' => sha1($notifiable->getEmailForVerification()),
        ]
    );

    // This amends the domain in the URL if core client user creating for another client - creates mismatch with signature though
    if(auth()->user()->client->is_core && auth()->user()->client_id != $notifiable->client_id) {
        $verifyUrl = Str::replace(auth()->user()->client->domains->first()->domain, $notifiable->client->domains->first()->domain, $verifyUrl);
    }

    return $verifyUrl;
});
4

1 回答 1

-1

签名取决于您的APP_KEY. 不应讨论将其发布到其他应用程序。这是不行的。

您可以使用它应该解决的密钥进行自定义。

将此代码粘贴到此处:

App\Providers\AppServiceProvider.php

public function register()
{
  $this->app->make('url')->setKeyResolver(function () {
    return $this->app->make('config')->get('app.shared-signed-key');
  });
}

此代码需要在coreclient和上,myclient以便他们可以生成和解析它。

您可以通过以下方法深入了解:

  • \Illuminate\Routing\UrlGenerator::signedRoute
  • \Illuminate\Routing\UrlGenerator::setKeyResolver
  • \Illuminate\Routing\RoutingServiceProvider::registerUrlGenerator
于 2021-12-15T23:25:08.213 回答