Login Symfony3守卫和登录表单
我尝试使用Guard来创建登录表单,而不是使用security.yml方式 Getuser和checkcredential正常。Login Symfony3守卫和登录表单,login,symfony,guard,Login,Symfony,Guard,我尝试使用Guard来创建登录表单,而不是使用security.yml方式 Getuser和checkcredential正常。 onAuthenticationSuccess正常(如果我在onAuthenticationSuccess中放入转储($token);die;,我可以在token中看到我的用户)并重定向到/acueil。 但是当它在/accueil上到达时,它会被发送回/login,因为用户身份验证始终处于启用状态 无法找到解决方案:c/ security.yml中的防火墙: f
onAuthenticationSuccess正常(如果我在onAuthenticationSuccess中放入
转储($token);die;
,我可以在token中看到我的用户)并重定向到/acueil。但是当它在/accueil上到达时,它会被发送回/login,因为用户身份验证始终处于启用状态 无法找到解决方案:c/ security.yml中的防火墙:
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_firewall:
pattern: ^/login$
anonymous: true
main:
pattern: ^/
anonymous: ~
logout: ~
switch_user: true
guard:
provider: database
authenticators:
- ent.login_authenticator
access_control:
- { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, roles: ROLE_ADMIN }
- { path: ^/, roles: ROLE_USER }
安全控制器
/**
* @Route("/login", name="login")
*
*/
public function loginAction(Request $request)
{
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return $this->redirectToRoute('accueil');
}
$authenticationUtils = $this->get('security.authentication_utils');
$exception = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('EntBundle::login.html.twig', [
'last_username' => $lastUsername,
'error' => $exception,
]);
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
// this controller will not be executed,
// as the route is handled by the Security system
}
登录验证人:
public function __construct(RouterInterface $router, UserPasswordEncoder $passwordEncoder, EntityManager $em) {
$this->router = $router;
$this->passwordEncoder = $passwordEncoder;
$this->em = $em;
}
public function getCredentials(Request $request)
{
if ($request->getPathInfo() != '/login_check' ) {
return null;
}
$request->getSession()->set(Security::LAST_USERNAME, $request->request->get('_username'));
return array(
'username' => $request->request->get('_username'),
'password' => $request->request->get('_password'),
);
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
try {
return $this->em->getRepository('EntBundle:User\User')->findOneBy(array('username' => $credentials ));
}
catch (UsernameNotFoundException $e) {
throw new CustomUserMessageAuthenticationException($this->failMessage);
}
}
public function checkCredentials($credentials, UserInterface $user) {
$plainPassword = $credentials['password'];
if ($this->passwordEncoder->isPasswordValid($user, $plainPassword)) {
return true;
}
throw new CustomUserMessageAuthenticationException($this->failMessage);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
// dump($token); die;
$url = $this->router->generate('accueil');
return new RedirectResponse($url);
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
$url = $this->router->generate('login');
return new RedirectResponse($url);
}
public function start(Request $request, AuthenticationException $authException = null)
{
$url = $this->router->generate('login');
return new RedirectResponse($url);
}
很抱歉响应太晚,在symfony3应用程序中设置令牌的代码如下所示:
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
and the actual setting of the token part will be like:
$token = new UsernamePasswordToken($user, $user->getPassword(), "firewall goes here for example: main", $user->getRoles());
$this->get("security.token_storage")->setToken($token);
我希望我能在这方面帮助您:)很抱歉响应太晚,在symfony3应用程序中设置令牌的代码如下所示:
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
and the actual setting of the token part will be like:
$token = new UsernamePasswordToken($user, $user->getPassword(), "firewall goes here for example: main", $user->getRoles());
$this->get("security.token_storage")->setToken($token);
我希望我在这方面帮了你:)你设置了用户令牌吗?嗨,Giovnni,谢谢你的回答。看来我错过了什么。您是否有一些链接解释如何管理此令牌?您是否设置了用户令牌?嗨,Giovnni,谢谢您的回答。看来我错过了什么。您是否有解释如何管理此令牌的链接?