来自外部应用程序的Joomla登录验证
我需要检查Joomla用户名和密码在我的外部应用程序中是否有效。用户无需仅在其帐户存在时登录系统。如何做到这一点?您的外部应用程序应该能够访问joomla应用程序的数据库,以在数据库中检查用户是否存在/是否有效。要检查它,您必须在外部应用程序中运行一些查询,以检查用户是否存在,如下所示:来自外部应用程序的Joomla登录验证,joomla,joomla1.5,Joomla,Joomla1.5,我需要检查Joomla用户名和密码在我的外部应用程序中是否有效。用户无需仅在其帐户存在时登录系统。如何做到这一点?您的外部应用程序应该能够访问joomla应用程序的数据库,以在数据库中检查用户是否存在/是否有效。要检查它,您必须在外部应用程序中运行一些查询,以检查用户是否存在,如下所示: $query = "select user_id from your_table where user_id = id_here"; // and more code afterwords. 我假设您的外部应
$query = "select user_id from your_table where user_id = id_here";
// and more code afterwords.
我假设您的外部应用程序可以访问Joomla的数据库,并且也是用php编写的 我已经回答了一个类似的问题,您可以使用相同的方法,但是您可以使用bind检查密码是否正确,而不是从JUser调用save方法 或者更好的方法:只需在创建一个文件后复制并粘贴Joomla自己的身份验证机制!检查JOOMLA_PATH/plugins/authentication/JOOMLA.php: 函数onAuthenticate$credentials、$options和$response{ jimport'joomla.user.helper'; //Joomla不喜欢空白密码 如果$credentials['password']为空{ $response->status=JAUTHENTICATE\u status\u失败; $response->error_message='不允许使用空密码'; 返回false; } //初始化变量 $conditions=; //获取数据库对象 $db=&JFactory::getDBO; $query='选择'id','password','gid'' “来自”用户“ .“WHERE username=”.$db->Quote$credentials['username'] ; $db->setQuery$query; $result=$db->loadObject; 如果$result{ $parts=分解“:”,$result->password; $crypt=$parts[0]; $salt=@$parts[1]; $testcrypt=JUserHelper::getCryptedPassword$credentials['password',$salt; 如果$crypt==$testcrypt{ $user=JUser::getInstance$result->id;//使其与系统的其余部分保持一致 $response->email=$user->email; $response->fullname=$user->name; $response->status=JAUTHENTICATE\u status\u SUCCESS; $response->error_message=; }否则{ $response->status=JAUTHENTICATE\u status\u失败; $response->error_message='Invalid password'; } } 否则{ $response->status=JAUTHENTICATE\u status\u失败; $response->error_message='用户不存在'; } }
您也可以编写一个API组件,这样您就可以使用url查询API并获得JSON响应 然后,您有一个控制器,它拾取此视图并调用模型或助手类,该类的代码如下所示。这样做的好处是,您可以将其保存在Joomla代码库中,并使其更加安全
function __construct() {
// params
$this->username = JRequest::getVar('user', '');
$this->password = JRequest::getVar('password', '');
$this->checkParameters();
}
private function checkParameters() {
// datatype checks
if ($this->username == '') {
die('ERROR: user is blank');
}
if ($this->password == '') {
die('ERROR: password is blank');
}
}
function execute() {
// Get the global JAuthentication object
jimport( 'joomla.user.authentication');
$auth = & JAuthentication::getInstance();
$credentials = array( 'username' => $this->username, 'password' => $this->password );
$options = array();
$response = $auth->authenticate($credentials, $options);
// success
return ($response->status === JAUTHENTICATE_STATUS_SUCCESS) {
$response->status = true;
} else {
// failed
$response->status = false;
}
echo json_encode($response);
}
作为@Martin答案的变体,组件可以返回与给定凭据关联的JUser对象。以下部件在Joomla 2.5中进行了测试: A view.raw.php: 在登录之前请注意注销。成功登录后,所有具有错误凭据的后续呼叫仍将返回第一次呼叫的用户,而不会注销!还要注意静默选项。这使得登录函数可以正常返回true或false,而不会产生异常。 controller.php: 注意原始格式。这需要让joomla返回纯json代码,而不是整个html页面。 可以通过带有url的ajax调用该组件: php?option=com\u extauth&task=view&format=raw&username=John&password=Doe 如果登录失败,返回的JSON对象将包含大多数字段的空值
完整组件是最简单的,基于com_hello,没有模型或tmpl。我不知道它有多安全,但我不喜欢创建API组件,并提出了以下建议:
<?php
define("LOGIN_USER_DOES_NOT_EXIST", 0);
define("LOGIN_USER_OK_INCORRECT_PASSWORD", 1);
define("LOGIN_USER_OK_PASSWORD_OK", 2);
define("LOGIN_NO_AUTH_SERVER", 3);
$user = $_GET["id"];
$userpassword = $_GET["pw"];
$mysql = mysql_connect("localhost", "login", "password") or die(LOGIN_NO_AUTH_SERVER);
mysql_select_db("joomladb") or die(LOGIN_NO_AUTH_SERVER);
$query = "Select password FROM j25_users WHERE username='" . $user . "'";
$result = mysql_query($query) or die(LOGIN_NO_AUTH_SERVER);
if (mysql_num_rows($result) != 1) {
$send = LOGIN_USER_DOES_NOT_EXIST;
} else {
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
$hashparts = explode(':', $col_value);
$userhash = md5($userpassword.$hashparts[1]);
if ($userhash == $hashparts[0]) $send = LOGIN_USER_OK_PASSWORD_OK;
else $send = LOGIN_USER_OK_INCORRECT_PASSWORD;
}
}
}
echo $send;
mysql_free_result($result);
mysql_close();
?>
结果只有一个字符可以被任何类型的应用程序轻松解析。但是Joomla会对密码进行奇怪的哈希运算,我可以尝试解构哈希,但如果可能的话,我宁愿只使用一个内部Jclass函数。是的,你必须使用它并弄清楚它是如何实现哈希运算的。
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
class ExtauthController extends JController
{
function display($cachable = false, $urlparams = false)
{
$view = JRequest::getVar( 'view', 'login' );
$layout = JRequest::getVar( 'layout', 'default' );
$format = JRequest::getVar( 'format', 'raw' );
$view = $this->getView( $view, $format );
$view->setLayout( $layout );
$view->display();
}
}
<?php
define("LOGIN_USER_DOES_NOT_EXIST", 0);
define("LOGIN_USER_OK_INCORRECT_PASSWORD", 1);
define("LOGIN_USER_OK_PASSWORD_OK", 2);
define("LOGIN_NO_AUTH_SERVER", 3);
$user = $_GET["id"];
$userpassword = $_GET["pw"];
$mysql = mysql_connect("localhost", "login", "password") or die(LOGIN_NO_AUTH_SERVER);
mysql_select_db("joomladb") or die(LOGIN_NO_AUTH_SERVER);
$query = "Select password FROM j25_users WHERE username='" . $user . "'";
$result = mysql_query($query) or die(LOGIN_NO_AUTH_SERVER);
if (mysql_num_rows($result) != 1) {
$send = LOGIN_USER_DOES_NOT_EXIST;
} else {
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
$hashparts = explode(':', $col_value);
$userhash = md5($userpassword.$hashparts[1]);
if ($userhash == $hashparts[0]) $send = LOGIN_USER_OK_PASSWORD_OK;
else $send = LOGIN_USER_OK_INCORRECT_PASSWORD;
}
}
}
echo $send;
mysql_free_result($result);
mysql_close();
?>