Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set X-Frame-Options in laravel project?

I want to prevent my website from clickJacking attack. In which file and where to set X-Frame-Options for preventing clickJacking attack.

like image 703
Darshan Prajapati Avatar asked May 28 '20 07:05

Darshan Prajapati


1 Answers

You have 2 ways:

  • Setup it in a reverse proxy such as Nginx
add_header X-Frame-Options "SAMEORIGIN";
  • Use Laravel middleware Illuminate\Http\Middleware\FrameGuard onto the routes you want to protect.
<?php

namespace Illuminate\Http\Middleware;

use Closure;

class FrameGuard
{
    /**
     * Handle the given request and get the response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $response->headers->set('X-Frame-Options', 'SAMEORIGIN', false);

        return $response;
    }
}
like image 52
Shizzen83 Avatar answered Sep 27 '22 21:09

Shizzen83