经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
确保Laravel网站不会被嵌入到其他站点中的方法
来源:jb51  时间:2019/10/18 14:01:48  对本文有异议

HTTP 响应头部中,有一个字段,叫做 X-Frame-Options,该字段可以用来指示是否允许自己的网站被嵌入到其他网站的 <iframe> 或者 <object> 标签中。该头部有三个值

  1. DENY - 始终不允许嵌入,即使是同一个域名
  2. SAMEORIGIN - 只能在相同域名中嵌入
  3. ALLOW-FROM uri - 设置允许的域

通常,可以在 HTTP 代理中进行配置,比如 nginx

add_header X-Frame-Options SAMEORIGIN;

Laravel 自带了用来「只允许同域名嵌入」的中间件,我们只需要在 /app/Http/Kernel.php 中添加即可

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

该中间件的实现如下

<?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;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛