8
11

More than 5 years have passed since last update.

LaravelでX-Frame-Optionsを設定する

Posted at

やり方が悪かったのか、.htaccessにHeader always append X-Frame-Options DENYってやっても効かなくて焦ったのでメモ。

1. Middlewareでサイト全体にかける

$ php artisan make:middleware ResponseHeader
app/Http/Middleware/ResponseHeader.php
  public function handle($request, Closure $next)
  {
    $response = $next($request);

    $response->header('X-Frame-Options', 'DENY');

    return $response;
  }

Kernelの$middlewareに登録

app/Http/Kernel.php
    protected $middleware = [
        \App\Http\Middleware\ResponseHeader::class,
    ];

2. アクションごとに設定する

return response()->view('hoge.index')->header('X-Frame-Options', 'DENY');

3. Middlewareで設定してコントローラで有効無効を出し分ける

Kernelの$routeMiddlewareに登録して、各コントローラのコンストラクタでコントロールする。(未検証)

8
11
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
8
11