简体   繁体   English

Drupal 9 - 仅允许从特定域访问页面

[英]Drupal 9 - Allow access to a page only from a specific domain

How to allow access to a Drupal file/page only from a specific domain?如何允许仅从特定域访问 Drupal 文件/页面?

I have tried php filter module but enabling this module can cause security and performance issues as it allows users to execute PHP code on your site.我已经尝试过 php 过滤器模块,但启用此模块会导致安全和性能问题,因为它允许用户在您的站点上执行 PHP 代码。

You can add an AccessChecker on your specific route and check if the domain is the correct one.您可以在您的特定路由上添加一个 AccessChecker,并检查域是否正确。

In your_module.services.yml:在你的_module.services.yml 中:

  your_module.domain_access_checker:
    class: Drupal\your_module\Access\DomainAccessChecker
    tags:
      - { name: access_check, applies_to: _acces_domain }

In your_module.routing.yml:在你的_module.routing.yml 中:

your_module.mypage:
  path: '/my-path'
  defaults:
    _title: 'My page'
    _controller: '\Drupal\your_module\Controller\MyController::show'
  requirements:
    _acces_domain : 'TRUE'

And finaly in your DomainAccessChecker.php:最后在您的 DomainAccessChecker.php 中:

  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
    $domainIsCorrect = your_code_here;
    if ($domainIsCorrect) {
      return AccessResult::allowed();
    }
    return AccessResult::forbidden();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM