gpt4 book ai didi

带有 Web 服务身份验证的 Drupal

转载 作者:行者123 更新时间:2023-12-04 06:04:31 24 4
gpt4 key购买 nike

我已经建立了一个简单的 Drupal 网站,并且即将完成。
该网站需要完全锁定,除了一个弹出框允许您登录,这很好,但登录不应该通过 Drupal 的标准身份验证,而是需要将用户名和密码传递给 Web 服务返回真或假,登录成功或失败。

我可以构建一个弹出窗口,但是如何通过 Web 服务路由身份验证以及打开 session 以停止直接链接而不登录。我需要检查的是该人是否已在父站点注册(谁提供网络服务)。

最佳答案

第 1 步是创建一个新模块。

我发现这个引用很有用:http://yetanotherprogrammingblog.com/node/14

该解决方案基于 drupal-6。 7有区别。

在模块中,您需要覆盖 hook_login_validate。例如

function mymodule_login_validate($form, &$form_state) {
$username = $form_state['values']['name'];

// do if condition to see if your authentication is required.
// e.g. you might want to keep the drupal administration users.
if (_mymodule_use_webservice_login($username)) {
// We are logging in using service.
if (_mymodule_validate_password($username, $form_state['values']['pass']) {
user_external_login_register($username, 'mymodule');
user_authenticate_finalize($form_state['values'];
} // else drop through to the end and return nothing - Drupal will handle the rejection for us
}
} else {
// Username is not an member number, so use standard Drupal authentication function
user_login_authenticate_validate($form, &$form_state);
}

在上面的注释中
  • 您必须在标题中创建带有 _use_webservice_login 的函数。测试是否去REST服务。我喜欢将这些放入 php 类中以保证命名空间的完整性。
  • 您需要创建执行凭据测试的函数。 _mymodule_validate_password。这将为您服务。
  • 按照正常的 drupal - 将“mymodule”更改为您创建的模块的名称,这样就不会发生命名空间冲突。

  • 我正在连接到一个 grails (spring/java) 应用程序来调用该服务。这主要决定了对make和变量的调用。我使用内置的 grails 身份验证服务进行身份验证并覆盖它以进行自定义身份验证。仅针对休息服务的调用应该类似。您需要一个 http 请求并解析响应。

    我的 PHP 类执行 _mymodule_validate_password 的片段是:
    $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
    'X-Requested-With' => 'XMLHttpRequest',
    );

    // _spring_security_remember_me
    $data = array(
    'j_username' => urlencode($username),
    'j_password' => urlencode($password),
    '_spring_security_remember_me' => 'on',
    );

    $query_string = http_build_query($data, '', '&');

    // WARNING - API changes in drupal 7
    // CAUTION - don't allow it to retry. Grails gives us a redirect which drupal_http_request follows, losing the cookie
    $response = drupal_http_request($endpoint_url, $headers, 'POST', $query_string, 0);

    // Did the initial login succeed.
    if ($response->code != 200 && $response->code != 302) {
    watchdog(self::module_name, self::module_name, 'Failed to log in %error.', array('%error' => $response->status_message));
    // FAIL
    return false;
    }

    // Initial login success. Follow the redirect.
    // Redirected request. Look for 200 && {"success":true,"username":"888"}
    $redirect_response = drupal_http_request($response->redirect_url, array(), 'GET', NULL);

    // The true success criteria
    if ($redirect_response->code == 200 && array_key_exists('Set-Cookie', $response->headers)) {
    // Authentication worked.
    $response_data = json_decode($redirect_response->data);
    if (property_exists($response_data, 'success') && $response_data->success &&
    property_exists($response_data, 'username')) {
    $login_success = true;
    } else {
    return false;
    }

    在一天结束时,我的请求给了我一个 JSON {"success": true, "username":"888"}。用户名是多余的。

    如果对提供数据的服务进行身份验证,Grails 会进行重定向。您可以只检查您是否从 Web 服务返回“200”并完成,或者只检查您的 JSON 数据中是否有“成功”的等价物。在上面的例子中,我们检查认证,记录重定向,然后调用重定向来获取认证数据。

    您可以从服务返回基本的 drupal 配置文件信息并设置详细信息。覆盖 mymodule_user 以访问更新后的回调。我会更新每次登录的详细信息,因为后端是主要的数据来源。本文顶部的引用使用“更新”。
    function mymodule_user($type, &$edit, &$account, $category = null) {
    switch ($type) {


    case 'login' :
    // Login should theoretically fail if the details don't get set.
    ... whatever you have to do to get the details. I call my class.
    user_save($account, array('mail' => $fedAuth->email_address));
    case 'logout' :
    // remove the grails cookies.

    }

    }

    根据您尝试做的事情,乐趣可能不会到此结束。就我而言,我必须更新自定义配置文件信息并保存客户端 cookie 以记录后续 Web 服务调用的登录详细信息。一些人认为这不是“安静的”,因为在 web 服务提供者中有一个 drupal session 和一个 session 。

    关于带有 Web 服务身份验证的 Drupal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8521112/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com