该密钥的生成取决于您正在访问的路由以及每次重新启动会话时都会更改的随机字符串。
因此,对于每次登录,您都会获得不同的会话密钥。
这种方法的缺点是您不能给其他人一个管理员网址并告诉他“嘿!看这里”,因为他们的会话密钥不同。
如果你想检查这个功能是如何实现的,请看Mage_Adminhtml_Model_Url::getUrl()中的以下代码:
$_route = $this->getRouteName() ? $this->getRouteName() : '*';
$_controller = $this->getControllerName() ? $this->getControllerName() : $this->getDefaultControllerName();
$_action = $this->getActionName() ? $this->getActionName() : $this->getDefaultActionName();
if ($cacheSecretKey) {
$secret = array(self::SECRET_KEY_PARAM_NAME => "\${$_controller}/{$_action}\$");
}
else {
$secret = array(self::SECRET_KEY_PARAM_NAME => $this->getSecretKey($_controller, $_action));
}
这是生成密钥的代码。深入了解getSecretKey 方法,您将看到:
public function getSecretKey($controller = null, $action = null)
{
$salt = Mage::getSingleton('core/session')->getFormKey();
$p = explode('/', trim($this->getRequest()->getOriginalPathInfo(), '/'));
if (!$controller) {
$controller = !empty($p[1]) ? $p[1] : $this->getRequest()->getControllerName();
}
if (!$action) {
$action = !empty($p[2]) ? $p[2] : $this->getRequest()->getActionName();
}
$secret = $controller . $action . $salt;
return Mage::helper('core')->getHash($secret);
}
所以密钥是由控制器名称、动作名称和$salt 以这种方式生成的Mage::getSingleton('core/session')->getFormKey(); 的散列构建@
getFormKey 方法如下所示(每个会话一个值):
public function getFormKey()
{
if (!$this->getData('_form_key')) {
$this->setData('_form_key', Mage::helper('core')->getRandomString(16));
}
return $this->getData('_form_key');
}