Bonjour, je suis un grand débutant sur Symfony et dans le cadre d'un stage je dois développer un site. Ce site doit contenir deux mdoes d'authentification possible.

La 1ère, grâce à une base de données ( que je gère avec FOSUserBundle avec l'ajout d'une authentification guard et qui fonctionnait très bien avant l'ajout de la seconde)
La 2nd, authentification CAS ( que je gère grâce au bundle d'un collègue, mais qui est absent pour l'instant ).

D'autre part c'est le 1er projet où on doit utiliser deux modes d'authentification en simultané, donc je suis en quelques sortes le cobaye, et forcément, je me retrouve avec un tas de problème que je ne sais pas gérer et personne ne peut m'aider, et je n'ai pas pu trouver mon bonheur sur internet ou la doc'.

Ca fait 5 bonnes heures que je suis ralenti par des problèmes qui m'énervent de plus en plus...


Donc après avoir ajouté le mode d'authentification CAS, je me retrouve avec un problème : Le mode d'authentificaiton avec la base de données ne fonctionne plus. Lorsque je rentre des identifiants d'utilisateurs de database, je me retrouve avec cette erreur:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.
Avant d'ajouter ce qui était relatif à l'authentification CAS, ça marchait niquel. J'ai juste suivi la doc' pour pouvoir utiliser deux modes d'authentification distincts.

J'ai cherché partout, j'ai demandé de l'aide partout où je pouvais, en vain. J'avoue que je suis au bout du rouleau, car l'ajout de l'authentification CAS a causé beaucoup de problèmes sur mon site en développement ( peut-être devrai-je créer d'autres sujets dans un futur proche au lieu de les étaler ici ?)

Security.yml:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt
 
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN
 
    providers:
        app:
           id: bes_auth.user_provider
 
    firewalls:
 
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
           # anonymous:    true
 
        public:
            pattern: ^accueil
            security: false
            anonymous: true
 
        main:
            logout_on_user_change: true
            pattern: ^/(admin|profile|packages|securiteInformatique|logout)
            #pattern: ^/(?!accueil).*$
 
            form_login:
                check_path: fos_user_security_login_check
                login_path: /login_check
 
            guard:
                authenticators:
                    - app.security.login_form_authenticator
                    - bes_auth.authenticator
 
                entry_point: Site\PagesBundle\Security\LoginFormAuthenticator
 
            logout:
                path:   deconnexion #nom de la route de déconnexion
                target: /
                success_handler: bes_auth.authenticator
            anonymous:    true
 
    access_control:
 
        #    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        #   - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        #  - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/accueil, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/.*, role: ROLE_SUPER_ADMIN }
        - { path: ^/profile/.*, role: ROLE_USER }
        - { path: ^/logout, role: IS_AUTHENTICATED_ANONYMOUSLY }

LoginFormAuthenticator.php:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
 
namespace Site\PagesBundle\Security;
 
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
 
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    use TargetPathTrait;
 
    private $em;
    private $router;
    private $passwordEncoder;
    private $csrfTokenManager;
 
    public function __construct(EntityManagerInterface $em, RouterInterface $router, UserPasswordEncoderInterface $passwordEncoder, CsrfTokenManagerInterface $csrfTokenManager)
    {
        $this->em = $em;
        $this->router = $router;
        $this->passwordEncoder = $passwordEncoder;
        $this->csrfTokenManager = $csrfTokenManager;
    }
 
    public function getCredentials(Request $request)
    {
        $isLoginSubmit = $request->getPathInfo() == '/login_check' && $request->isMethod('POST');
        if (!$isLoginSubmit) {
            // skip authentication
            return;
        }
 
        $username = $request->request->get('_username');
        $password = $request->request->get('_password');
        $csrfToken = $request->request->get('_csrf_token');
 
        if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken('authenticate', $csrfToken))) {
            throw new InvalidCsrfTokenException('Invalid CSRF token.');
        }
 
        $request->getSession()->set(
            Security::LAST_USERNAME,
            $username
        );
 
        return [
            'username' => $username,
            'password' => $password,
        ];
    }
 
    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $username = $credentials['username'];
 
        return $this->em->getRepository('PagesBundle:User')
            ->findOneBy(['username' => $username]);
    }
 
    public function checkCredentials($credentials, UserInterface $user)
    {
        $password = $credentials['password'];
 
        if ($this->passwordEncoder->isPasswordValid($user, $password)) {
            return true;
        }
 
        return false;
    }
 
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        $targetPath = null;
 
        // if the user hit a secure page and start() was called, this was
        // the URL they were on, and probably where you want to redirect to
        $targetPath = $this->getTargetPath($request->getSession(), $providerKey);
 
        if (!$targetPath) {
            $targetPath = $this->router->generate('homepage');
        }
 
        return new RedirectResponse($targetPath);
    }
 
    protected function getLoginUrl()
    {
        return $this->router->generate('connexion_index');
    }
}
Et mon fichier de connexion permettant aux utilisateurs inscrits dans une base de données, de se connecter ( qui va inclure le formulaire de connexion de FOSUserBundle) :

connexion.html.twig:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{% extends "@FOSUser/layout.html.twig" %}
{% block body %}
 
{% block content_fos_user %}
<div class="form-login">
    {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
    {#{{ app.user.username }}  <a href="{{ path('fos_user_security_logout') }}">Déconnexion</a>#}
    {% else %}
        {{ render(controller('FOSUserBundle:Security:login')) }}
    {% endif %}
</div>
{% endblock %}
 
<br/><br/>
 
{% for uneInfo in listeInfos %}
 
 
<h1><b><u>{{uneInfo.titre}} : </u></b></h1>
<br/><br/>
 
<div class="content">
    {{uneInfo.contenu|raw}}
</div>
<div class="metadata">
    <h6><i>Publié le {{uneInfo.updatedAt|date('d-m-Y')}} à {{uneInfo.updatedAt|date('H:i')}}</i></h6>
</div>
 
<br/><br/><br/><br/><br/>
{% endfor %}
 
{% endblock %}
Voilà, je vous remercie par avance pour votre aide, car là je vais exploser