简体   繁体   English

Typo3-手动登录仅适用于cookie

[英]Typo3 - manual login only works with cookie

I'm trying to manually login the user using this snippet (after verifying the login data of course): 我正在尝试使用此代码段手动登录用户(当然要验证登录数据之后):

public function loginUser($user) {
    $userArray = array('uid' => $user->getUid());
    $GLOBALS['TSFE']->fe_user->is_permanent = true;
    $GLOBALS['TSFE']->fe_user->checkPid = 0;
    $GLOBALS['TSFE']->fe_user->createUserSession($userArray);
    $GLOBALS['TSFE']->fe_user->user = $GLOBALS['TSFE']->fe_user->fetchUserSession();
    $GLOBALS['TSFE']->fe_user->fetchGroupData();
    $GLOBALS['TSFE']->loginUser = true;
    //this somehow forces a cookie to be set
    $GLOBALS['TSFE']->fe_user->setAndSaveSessionData('dummy', TRUE);
}

If I leave out the "setAndSaveSessionData" Part, the login doesn't work at all, after a page redirect the login data are gone and the user is logged out again. 如果我忽略了“ setAndSaveSessionData”部分,则登录根本不起作用,在页面重定向之后,登录数据就消失了,并且用户再次注销。 But the setAndSaveSessionData stores the session in a cookie and the user will remain logged in even after closing the browser - which is a behaviour I do not want (not without the user's consent). 但是setAndSaveSessionData将会话存储在cookie中,即使关闭浏览器后,用户仍将保持登录状态-这是我不希望的行为(未经用户同意)。 Is there a way to manually login the user without the "setAndSaveSessionData" part? 没有“ setAndSaveSessionData”部分,有没有办法手动登录用户? I'm using Typo3 6.2.12 with extbase and felogin 我正在将Typo3 6.2.12与extbase和felogin一起使用

Thank you very much in advance! 提前非常感谢您!

In some TYPO3 6.2.x (I don't remember which x exactly) there was a change introduced, which causes that you need to call AbstractUserAuthentication::setSessionCookie() method yourself... Unfortunately it has protected access so the best way to login user is creating some util class extending it: 在某些TYPO3 6.2.x(我不记得确切是哪个x )中引入了一个更改,这导致您需要自己调用AbstractUserAuthentication::setSessionCookie()方法...不幸的是,它具有protected访问权限,因此最好的方法是登录用户正在创建一些util类来扩展它:

typo3conf/your_ext/Classes/Utils/FeuserAuthentication.php typo3conf / your_ext /班/ utils的/ FeuserAuthentication.php

<?php

namespace VendorName\YourExt\Utils;


use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;

class FeuserAuthentication extends AbstractUserAuthentication {


    function __construct($uid) {
        return $this->authByUid($uid);
    }

    // Authenticates user by uid
    public function authByUid($uid) {

        /** @var $fe_user FrontendUserAuthentication */
        $fe_user = $GLOBALS['TSFE']->fe_user;
        $fe_user->createUserSession(array('uid' => $uid));
        $fe_user->user = $fe_user->getRawUserByUid($uid);
        $fe_user->fetchGroupData();
        $GLOBALS['TSFE']->loginUser = true;
        $fe_user->setSessionCookie();

        return $fe_user->isSetSessionCookie();

    }

}

so to login your user by uid you just need to create new object of this class with $uid of fe_user as a constructor's param: 因此,要通过uid登录用户,您只需使用fe_user $uid作为构造函数的参数创建此类的新对象:

$this->objectManager->get(
    '\VendorName\YourExt\Utils\FeuserAuthentication', 
    $user->getUid()
);

PS As you can see this class doesn't check if account exists and/or is enabled, so you need to check it yourself before authentication attempt. PS如您所见,此类不会检查帐户是否存在和/或已启用,因此您需要在进行身份验证之前亲自检查一下。

A website is delivered HTTP(S), which is a stateless protocol. 网站交付了HTTP(S),这是一种无状态协议。 This means that something has to be saved on the client computer, because otherwise the server couldn't reliably recognize the user again. 这意味着必须在客户端计算机上保存某些内容 ,否则服务器将无法再次可靠地识别用户。 There are several ways to do this: 做这件事有很多种方法:

  • Use a session cookie (The way TYPO3 does it, PHP also does that) 使用会话cookie(TYPO3这样做的方式,PHP也这样做)
  • Manually add a session ID to each request on a page, Java-based applications do that sometimes. 手动将会话ID添加到页面上的每个请求,基于Java的应用程序有时会这样做。 This breaks if the user leaves the page and comes back later (in the same session, or in another tab without copying a link). 如果用户离开页面并稍后返回(在同一会话中,或者在另一个选项卡中而不复制链接),则会中断操作。
  • There are probably some more ways, but I can't come up with them right now 可能还有更多方法,但是我现在无法提出它们

So my answer is: Since I believe it is hard to get TYPO3 to switch to another way of setting the session information (would be quite user unfriendly), there is no good way to avoid setting the cookie. 因此,我的回答是:由于我认为很难让TYPO3切换到另一种设置会话信息的方式(会对用户不友好),因此没有避免设置cookie的好方法。

However: 然而:
The cookie is a session cookie, which expires when the browser session is ended. cookie是会话cookie,当浏览器会话结束时该cookie会过期。 So closing the browser and reopening it should end the login, except if the browser restores the previous session when opened. 因此,关闭浏览器并重新打开它应结束登录, 除非浏览器在打开时恢复上一个会话。 Many modern browsers do this, especially mobile browsers. 许多现代浏览器都这样做,尤其是移动浏览器。 Ways to get around that: 解决该问题的方法:

  • Use the incognito or private mode of the browser 使用浏览器的隐身或私有模式
  • Set the browser to start a fresh session on each start and make sure the browser terminates when you are finished (again: watch mobile devices). 将浏览器设置为在每次启动时重新启动会话,并确保浏览器在完成后终止(再次:观看移动设备)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM