我已經建立了一個簡單的Drupal網站並即將完成。
這個網站需要完全鎖定,除了一個彈出框,允許你登錄,這很好,但登錄不應該通過Drupal的標準認證,而是用戶名和密碼需要傳遞給Web服務返回true或false,登錄成功或失敗。帶Web服務認證的Drupal
我可以構建一個彈出式窗口,但是如何通過Web服務路由身份驗證,還需要打開一個會話以在不登錄的情況下停止直接鏈接。我需要檢查的是該人員已向父級註冊(誰提供了Web服務)。
我已經建立了一個簡單的Drupal網站並即將完成。
這個網站需要完全鎖定,除了一個彈出框,允許你登錄,這很好,但登錄不應該通過Drupal的標準認證,而是用戶名和密碼需要傳遞給Web服務返回true或false,登錄成功或失敗。帶Web服務認證的Drupal
我可以構建一個彈出式窗口,但是如何通過Web服務路由身份驗證,還需要打開一個會話以在不登錄的情況下停止直接鏈接。我需要檢查的是該人員已向父級註冊(誰提供了Web服務)。
第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);
}
在上面的注意,
我正在連接到一個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 {「成功」:真正的「用戶名」:「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會話和一個會話。