This repository was archived by the owner on Jun 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 15.4: Configuring the Security Authentication
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; | ||
|
||
class SecurityController extends AbstractController | ||
{ | ||
/** | ||
* @Route("/login", name="app_login") | ||
*/ | ||
public function login(AuthenticationUtils $authenticationUtils): Response | ||
{ | ||
// if ($this->getUser()) { | ||
// return $this->redirectToRoute('target_path'); | ||
// } | ||
|
||
// get the login error if there is one | ||
$error = $authenticationUtils->getLastAuthenticationError(); | ||
// last username entered by the user | ||
$lastUsername = $authenticationUtils->getLastUsername(); | ||
|
||
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]); | ||
} | ||
|
||
/** | ||
* @Route("/logout", name="app_logout") | ||
*/ | ||
public function logout(): void | ||
{ | ||
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\Security; | ||
|
||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Security; | ||
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | ||
use Symfony\Component\Security\Http\Util\TargetPathTrait; | ||
|
||
class AppAuthenticator extends AbstractLoginFormAuthenticator | ||
{ | ||
use TargetPathTrait; | ||
|
||
public const LOGIN_ROUTE = 'app_login'; | ||
|
||
private UrlGeneratorInterface $urlGenerator; | ||
|
||
public function __construct(UrlGeneratorInterface $urlGenerator) | ||
{ | ||
$this->urlGenerator = $urlGenerator; | ||
} | ||
|
||
public function authenticate(Request $request): Passport | ||
{ | ||
$username = $request->request->get('username', ''); | ||
|
||
$request->getSession()->set(Security::LAST_USERNAME, $username); | ||
|
||
return new Passport( | ||
new UserBadge($username), | ||
new PasswordCredentials($request->request->get('password', '')), | ||
[ | ||
new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')), | ||
] | ||
); | ||
} | ||
|
||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | ||
{ | ||
if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) { | ||
return new RedirectResponse($targetPath); | ||
} | ||
|
||
return new RedirectResponse($this->urlGenerator->generate('admin')); | ||
} | ||
|
||
protected function getLoginUrl(Request $request): string | ||
{ | ||
return $this->urlGenerator->generate(self::LOGIN_ROUTE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{% extends 'base.html.twig' %} | ||
|
||
{% block title %}Log in!{% endblock %} | ||
|
||
{% block body %} | ||
<form method="post"> | ||
{% if error %} | ||
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div> | ||
{% endif %} | ||
|
||
{% if app.user %} | ||
<div class="mb-3"> | ||
You are logged in as {{ app.user.username }}, <a href="{{ path('app_logout') }}">Logout</a> | ||
</div> | ||
{% endif %} | ||
|
||
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1> | ||
<label for="inputUsername">Username</label> | ||
<input type="text" value="{{ last_username }}" name="username" id="inputUsername" class="form-control" autocomplete="username" required autofocus> | ||
<label for="inputPassword">Password</label> | ||
<input type="password" name="password" id="inputPassword" class="form-control" autocomplete="current-password" required> | ||
|
||
<input type="hidden" name="_csrf_token" | ||
value="{{ csrf_token('authenticate') }}" | ||
> | ||
|
||
{# | ||
Uncomment this section and add a remember_me option below your firewall to activate remember me functionality. | ||
See https://symfony.com/doc/current/security/remember_me.html | ||
<div class="checkbox mb-3"> | ||
<label> | ||
<input type="checkbox" name="_remember_me"> Remember me | ||
</label> | ||
</div> | ||
#} | ||
|
||
<button class="btn btn-lg btn-primary" type="submit"> | ||
Sign in | ||
</button> | ||
</form> | ||
{% endblock %} |