Bonjour,
J'ai un problème avec la sécurité de Symfony2. J'ai un utilisateur admin avec le mot de passe admin mais dès que j'essaye de m'y connecter j'ai le message suivant : The presented password is invalid.. en revanche si j'essaie avec un utilisateur bidon j'ai bien Bad Credential
J'ai mon entité Utilisateur qui implemente UserInterface
et mon entité Role qui implemente RoleInterface
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 class Utilisateur implements UserInterface { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $username * * @ORM\Column(name="username", type="string", length=255) */ private $username; /** * @var string $password * * @ORM\Column(name="password", type="string", length=255) */ private $password; /** * @var string $email * * @ORM\Column(name="email", type="string", length=255) */ private $email; /** * @var string $salt * * @ORM\Column(name="salt", type="string", length=255) */ private $salt; /** * @ORM\ManyToMany(targetEntity="Role") */ private $userRoles; /** * Consturcts a new instance of Role. */ public function __construct() { $this->userRoles = new ArrayCollection(); } /** * Gets an array of roles. * * @return array An array of Role objects */ public function getRoles() { return $this->getUserRoles()->toArray(); } /** * Gets the user roles. * * @return ArrayCollection A Doctrine ArrayCollection */ public function getUserRoles() { return $this->userRoles; } /** * * @param UserInterface $user The user * @return boolean True if equal, false othwerwise. */ public function equals(UserInterface $user) { return md5($this->getUsername()) == md5($user->getUsername()); } public function getSalt() { return $this->salt; } public function setSalt($value) { $this->salt = $value; } /** * Gets the username. * * @return string The username. */ public function getUsername() { return $this->username; } /** * Sets the username. * * @param string $value The username. */ public function setUsername($value) { $this->username = $value; } /** * Erases the user credentials. */ public function eraseCredentials() { } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set password * * @param string $password */ public function setPassword($password) { $this->password = $password; } /** * Get password * * @return string */ public function getPassword() { return $this->password; } /** * Set email * * @param string $email */ public function setEmail($email) { $this->email = $email; } /** * Get email * * @return string */ public function getEmail() { return $this->email; } }
J'ai defini mon security.yml de la manière suivante :
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 class Role implements RoleInterface { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string $name * * @ORM\Column(name="name", type="string", length=255) */ protected $name; /** * Gets the id. * * @return integer The id. */ public function getId() { return $this->id; } /** * Gets the role name. * * @return string The name. */ public function getName() { return $this->name; } /** * Sets the role name. * * @param string $value The name. */ public function setName($value) { $this->name = $value; } /** * Gets the DateTime the role was created. * * @return DateTime A DateTime object. */ public function getCreatedAt() { return $this->createdAt; } /** * Consturcts a new instance of Role. */ public function __construct() { $this->createdAt = new \DateTime(); } /** * Implementation of getRole for the RoleInterface. * * @return string The role. */ public function getRole() { return $this->getName(); } }
Ici j'ai mis en plaintext le mot de passe mais si je met md5 j'ai le même pb (en changant biensur aussi la valeur dans la bdd)
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 security: encoders: Anax\GortozigBundle\Entity\Utilisateur: plaintext role_hierarchy: ROLE_MEDECIN: ROLE_USER ROLE_SECRETAIRE: ROLE_MEDECIN ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_MEDECIN, ROLE_ALLOWED_TO_SWITCH] providers: main: entity: { class: Anax\GortozigBundle\Entity\Utilisateur, property: username } firewalls: secured_area: pattern: ^/ form_login: check_path: /login_check login_path: /login anonymous: true logout: true access_control: - { path: ^/utilisateur, roles: ROLE_SECRETAIRE } - { path: ^/evenement, roles: ROLE_MEDECIN } - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY } hide_user_not_found: false
et ma vue pour le login :
et mon action de login :
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 <form action=" {{ path('_security_check') }}" method="post"> {% if error %} <div class="error">{{ error.message }}</div> {% endif %} <form action="{{ path('_security_check') }}" method="POST"> <div> <label for="username">Username:</label> <input type="text" id="username" name="_username" value="{{ last_username }}" /> </div> <div> <label for="password">Password:</label> <input type="password" id="password" name="_password" /> </div> <input type="submit" name="login" value="submit" /> </form>
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 public function loginAction() { $request = $this->getRequest(); $session = $request->getSession(); // get the login error if there is one if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); } else { $error = $session->get(SecurityContext::AUTHENTICATION_ERROR); } return $this->render('AnaxGortozigBundle:Default:login.html.twig', array( // last username entered by the user 'last_username' => $session->get(SecurityContext::LAST_USERNAME), 'error' => $error, )); }
Quelqu'un sait pourquoi j'ai ce message d'erreur?
Merci
Partager