Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1eb5f23

Browse files
committed
Updating some places to use the new CustomUserMessageAuthenticationException
1 parent5330c43 commit1eb5f23

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

‎cookbook/security/api_key_authentication.rst‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ value and then a User object is created::
2525
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
2626
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2727
use Symfony\Component\Security\Core\Exception\AuthenticationException;
28+
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
2829
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
2930
use Symfony\Component\HttpFoundation\Request;
3031
use Symfony\Component\Security\Core\User\UserProviderInterface;
@@ -69,7 +70,8 @@ value and then a User object is created::
6970
$username = $userProvider->getUsernameForApiKey($apiKey);
7071

7172
if (!$username) {
72-
throw new AuthenticationException(
73+
// this message will be returned to the client
74+
throw new CustomUserMessageAuthenticationException(
7375
sprintf('API Key "%s" does not exist.', $apiKey)
7476
);
7577
}
@@ -90,6 +92,11 @@ value and then a User object is created::
9092
}
9193
}
9294

95+
..versionadded::2.8
96+
The ``CustomUserMessageAuthenticationException`` class is new in Symfony 2.8
97+
and helps you return custom authentication messages. In 2.7 or earlier, throw
98+
an ``AuthenticationException`` or any sub-class (you can still do this in 2.8).
99+
93100
Once you've:ref:`configured<cookbook-security-api-key-config>` everything,
94101
you'll be able to authenticate by adding an apikey parameter to the query
95102
string, like ``http://example.com/admin/foo?apikey=37b51d194a7513e45b56f6524f2d51f2``.
@@ -280,7 +287,11 @@ you can use to create an error ``Response``.
280287
281288
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
282289
{
283-
return new Response("Authentication Failed.", 403);
290+
return new Response(
291+
// this contains information about *why* authentication failed
292+
// use it, or return your own message
293+
strtr($exception->getMessageKey(), $exception->getMessageData())
294+
, 403)
284295
}
285296
}
286297
@@ -532,7 +543,8 @@ to see if the stored token has a valid User object that can be used::
532543
}
533544

534545
if (!$username) {
535-
throw new AuthenticationException(
546+
// this message will be returned to the client
547+
throw new CustomUserMessageAuthenticationException(
536548
sprintf('API Key "%s" does not exist.', $apiKey)
537549
);
538550
}

‎cookbook/security/custom_password_authenticator.rst‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ the user::
2929
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
3030
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
3131
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
32-
use Symfony\Component\Security\Core\Exception\AuthenticationException;
32+
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
3333
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
3434
use Symfony\Component\Security\Core\User\UserProviderInterface;
3535

@@ -47,15 +47,17 @@ the user::
4747
try {
4848
$user = $userProvider->loadUserByUsername($token->getUsername());
4949
} catch (UsernameNotFoundException $e) {
50-
throw new AuthenticationException('Invalid username or password');
50+
// error will be shown to the client
51+
throw new CustomUserMessageAuthenticationException('Invalid username or password');
5152
}
5253

5354
$passwordValid = $this->encoder->isPasswordValid($user, $token->getCredentials());
5455

5556
if ($passwordValid) {
5657
$currentHour = date('G');
5758
if ($currentHour < 14 || $currentHour > 16) {
58-
throw new AuthenticationException(
59+
// error will be shown to the client
60+
throw new CustomUserMessageAuthenticationException(
5961
'You can only log in between 2 and 4!',
6062
100
6163
);
@@ -69,7 +71,8 @@ the user::
6971
);
7072
}
7173

72-
throw new AuthenticationException('Invalid username or password');
74+
// error will be shown to the client
75+
throw new CustomUserMessageAuthenticationException('Invalid username or password');
7376
}
7477

7578
public function supportsToken(TokenInterface $token, $providerKey)
@@ -84,6 +87,11 @@ the user::
8487
}
8588
}
8689

90+
..versionadded::2.8
91+
The ``CustomUserMessageAuthenticationException`` class is new in Symfony 2.8
92+
and helps you return custom authentication messages. In 2.7 or earlier, throw
93+
an ``AuthenticationException`` or any sub-class (you can still do this in 2.8).
94+
8795
How it Works
8896
------------
8997

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp