You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drupal 8使用r4032login模块后登录按destination参数跳转问题咨询

问题解决方案

第一步:优先排查r4032login模块配置

  • 登录Drupal后台,进入「配置」→「系统」→「403 to Login」设置页面
  • 确认勾选 保留原始目标路径 选项
  • 检查「默认登录后跳转路径」配置项,若此处填写了站点首页地址,请清空后保存配置,该配置优先级高于destination参数

提示:如果配置完r4032login后仍不生效,可先禁用其他与登录重定向相关的第三方模块,排除模块优先级冲突问题。

第二步:修复自定义跳转代码逻辑

你现有的自定义代码存在两处核心问题:

  1. 没有读取URL中的destination参数,硬编码了路由地址
  2. 自定义提交handler追加到了提交队列末尾,会被Drupal核心的登录重定向逻辑覆盖

以下是可直接使用的正确代码:

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function test_redirect_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // 将自定义提交 handler 插入到提交队列最前面,避免被核心逻辑覆盖
  array_unshift($form['#submit'], 'test_redirect_user_login_form_submit');
}

/**
 * Custom submit handler for the login form.
 */
function test_redirect_user_login_form_submit($form, FormStateInterface $form_state) {
  $request = \Drupal::request();
  // 从请求参数中读取 destination
  $destination = $request->query->get('destination');
  if (!empty($destination)) {
    // 自动校验路径合法性,避免开放重定向漏洞
    $url = Url::fromUserInput('/' . ltrim($destination, '/'));
    if ($url->access()) {
      $form_state->setRedirectUrl($url);
      return;
    }
  }
  // 兜底逻辑:没有合法destination时跳转到用户个人页
  $form_state->setRedirect('entity.user.canonical', ['user' => \Drupal::currentUser()->id()]);
}

验证方式

修改后清除Drupal缓存,访问/user/login?destination=my-modules测试登录跳转即可。


内容的提问来源于stack exchange,提问作者Mohamed Amine Bouhadjar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 16:15:03