[英]Symfony don't save manual login
我已经创建了一个手动的代码内登录方法。 页面上的功能有效。
探查器说我已正确认证。
但是Symfony不会正确保存会话/登录。 如果我访问一个页面,它们仅适用于ROLE_USER,我将获得Full authentication is required to access this resource.
消息和先前登录中的用户未保存(Symfony使用匿名令牌)。
这是我的登录操作:
namespace ###HIDDEN###\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\User;
class AutoLoginController extends Controller
{
/**
* @Route("/auto-login")
*/
public function indexAction(Request $request)
{
if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
$user = new User("Marcel", null, array('ROLE_USER'));
$token = new UsernamePasswordToken($user, null, "main", $user->getRoles());
$this->get("security.token_storage")->setToken($token);
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
return new Response("<body>Logging in!</body>");
}
return new Response("<body>You already logged in!</body>");
}
}
我正在使用Symfony 2.8 LTS版本。
确保您的User
类实现了UserInterface
( 文档 )。
它必须返回用户具有的角色,这些角色可以是静态的,也可以是从数据库等持久性存储中设置的。
还要记住检查Web Profiler以查看身份验证详细信息以及当前已身份验证用户的已授予角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.