LoginSignup
7
2

More than 3 years have passed since last update.

Laravelメールアドレス確認メールでhttpsだと403 | Invalid signatureで認証できない問題への対応

Posted at

メールアドレス確認のURLをhttpsにする

商用環境はhttpsで運用されると思います。
デフォルトでは、httpでメールアドレス確認のURLが生成されてしまいますので、
以下の対応を行います。

/app/Providers/AppServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

use Illuminate\Routing\UrlGenerator;

class AppServiceProvider extends ServiceProvider
{
    public function boot(UrlGenerator $url)
    {
      # 開発環境(local)はhttpなので、httpsにしない
      if (config('app.env') !== 'local') {
        $url->forceScheme('https');
      }
    }
}

AppServiceProvider.phpで、bootにUrlGeneratorを渡して、
urlがhttpsで生成されるようにしています。

これで、メールアドレス確認メールのhttps化は完了です。

確認してみると403 | Invalid signature

メールアドレス確認URLは、httpのままだと成功しましたが、httpsにすると失敗します。

メールアドレス確認URLで、403 | Invalid signatureを解決する

解決策として、TrustedProxyでプロキシを'*'に設定します。
IPがわかる時など、全てを許可したくないときは、以下ドキュメントを参考にプロキシを設定しましょう。
https://readouble.com/laravel/5.8/ja/requests.html#configuring-trusted-proxies

/app/Http/Middleware/TrustProxies.php
<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies = '*'; //これを'*'にするだけ

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;
}

解決時の環境について

この問題を解決時の私のLaravelバージョンは5.8.13です。
デプロイ先はHerokuです。

まとめ

  1. url生成をhttpsに変更する
  2. IPアドレスが分からないときは、全プロキシを信用させる

参考にしたページ

url生成をhttpsにする

今回の問題の解決

同じと思われる問題に直面してる例

Laravelドキュメントの信用するプロキシの設定

7
2
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
7
2