我已经创建了一个手动的代码登录方法。页面上的函数可以工作。
分析人员说我的身份是正确的。
但是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>");
}
}我正在使用Symfony2.8LTS版本。
发布于 2016-06-04 13:25:03
确保您的User类实现了UserInterface (文档)。
它必须返回用户所具有的角色,无论是静态的还是从持久存储(如数据库)设置的。
还记得检查Web以查看身份验证细节,以及授予当前经过身份验证的用户的角色。
https://stackoverflow.com/questions/37630051
复制相似问题