1
我嘗試使用Guard來創建登錄表單而不是security.yml方式。Symfony3 Guard和登錄表單
Getuser和checkcredential都可以。
onAuthenticationSuccess是好的(如果我把onOuthenticationSuccess中的dump($token); die;
我可以在令牌中看到我的用戶)並重定向到/ accueil。
但是,當它到達/ accueil它發回/登錄,因爲用戶身份驗證始終是匿名的!
不可能找到一個解決方案: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 }
securityController
/**
* @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
}
loginAuthenticator:
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);
}
你設置的用戶令牌? –
嗨Giovnni謝謝你的答案。 看起來像有一些我錯過了。你有解釋如何管理這個令牌的鏈接嗎? – lemairep