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

Commit9f032d7

Browse files
[Security] add "lazy_authentication" mode to firewalls
1 parentf834c92 commit9f032d7

File tree

13 files changed

+352
-4
lines changed

13 files changed

+352
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Bundle\SecurityBundle\DependencyInjection\Compiler;
13+
14+
useSymfony\Bridge\Monolog\Processor\ProcessorInterface;
15+
useSymfony\Component\DependencyInjection\Argument\BoundArgument;
16+
useSymfony\Component\DependencyInjection\ContainerBuilder;
17+
useSymfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
useSymfony\Component\DependencyInjection\Reference;
19+
useSymfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
20+
21+
/**
22+
* Adds a rule to bind "security.actual_token_storage" to ProcessorInterface instances.
23+
*
24+
* @author Nicolas Grekas <p@tchwork.com>
25+
*/
26+
class RegisterForAutoconfigurationPassimplements CompilerPassInterface
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
publicfunctionprocess(ContainerBuilder$container)
32+
{
33+
if ($container->has('security.actual_token_storage')) {
34+
$processorAutoconfiguration =$container->registerForAutoconfiguration(ProcessorInterface::class);
35+
$processorAutoconfiguration->setBindings($processorAutoconfiguration->getBindings() +array(
36+
TokenStorageInterface::class =>newBoundArgument(newReference('security.actual_token_storage'),false),
37+
));
38+
}
39+
}
40+
}

‎src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
196196
->scalarNode('entry_point')->end()
197197
->scalarNode('provider')->end()
198198
->booleanNode('stateless')->defaultFalse()->end()
199+
->booleanNode('lazy_authentication')->defaultFalse()->end()
199200
->scalarNode('context')->cannotBeEmpty()->end()
200201
->booleanNode('logout_on_user_change')
201202
->defaultTrue()

‎src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ private function createFirewalls($config, ContainerBuilder $container)
204204
list($matcher,$listeners,$exceptionListener,$logoutListener) =$this->createFirewall($container,$name,$firewall,$authenticationProviders,$providerIds,$configId);
205205

206206
$contextId ='security.firewall.map.context.'.$name;
207-
$context =$container->setDefinition($contextId,newChildDefinition('security.firewall.context'));
207+
$context =newChildDefinition($firewall['stateless'] || !$firewall['lazy_authentication'] ?'security.firewall.context' :'security.firewall.lazy_context');
208+
$context =$container->setDefinition($contextId,$context);
208209
$context
209210
->replaceArgument(0,newIteratorArgument($listeners))
210211
->replaceArgument(1,$exceptionListener)
@@ -374,7 +375,9 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
374375
}
375376

376377
// Access listener
377-
$listeners[] =newReference('security.access_listener');
378+
if ($firewall['stateless'] || !$firewall['lazy_authentication']) {
379+
$listeners[] =newReference('security.access_listener');
380+
}
378381

379382
// Exception listener
380383
$exceptionListener =newReference($this->createExceptionListener($container,$firewall,$id,$configuredEntryPoint ?:$defaultEntryPoint,$firewall['stateless']));

‎src/Symfony/Bundle/SecurityBundle/Resources/config/collectors.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<serviceid="data_collector.security"class="Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector">
1111
<tagname="data_collector"template="@Security/Collector/security.html.twig"id="security"priority="270" />
12-
<argumenttype="service"id="security.token_storage"on-invalid="ignore" />
12+
<argumenttype="service"id="security.actual_token_storage" />
1313
<argumenttype="service"id="security.role_hierarchy" />
1414
<argumenttype="service"id="security.logout_url_generator" />
1515
<argumenttype="service"id="security.access.decision_manager" />

‎src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
</service>
2222
<serviceid="Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface"alias="security.authorization_checker" />
2323

24-
<serviceid="security.token_storage"class="Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage"public="true">
24+
<serviceid="security.token_storage"class="Symfony\Component\Security\Core\Authentication\Token\Storage\LazyTokenStorage"public="true">
2525
<tagname="kernel.reset"method="setToken" />
26+
<argumenttype="service"id="security.actual_token_storage" />
2627
</service>
2728
<serviceid="Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface"alias="security.token_storage" />
2829

30+
<serviceid="security.actual_token_storage"class="Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage" />
31+
2932
<serviceid="security.helper"class="Symfony\Component\Security\Core\Security">
3033
<argumenttype="service_locator">
3134
<argumentkey="security.token_storage"type="service"id="security.token_storage" />
@@ -145,6 +148,14 @@
145148
<argument /><!-- FirewallConfig-->
146149
</service>
147150

151+
<serviceid="security.firewall.lazy_context"class="Symfony\Bundle\SecurityBundle\Security\LazyFirewallContext"abstract="true">
152+
<argumenttype="collection" />
153+
<argumenttype="service"id="security.exception_listener" />
154+
<argument /><!-- LogoutListener-->
155+
<argument /><!-- FirewallConfig-->
156+
<argumenttype="service"id="security.lazy_access_listener" />
157+
</service>
158+
148159
<serviceid="security.firewall.config"class="Symfony\Bundle\SecurityBundle\Security\FirewallConfig"abstract="true">
149160
<argument /><!-- name-->
150161
<argument /><!-- user_checker-->

‎src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,13 @@
242242
<argumenttype="service"id="security.access_map" />
243243
<argumenttype="service"id="security.authentication.manager" />
244244
</service>
245+
246+
<serviceid="security.lazy_access_listener"class="Symfony\Component\Security\Http\Firewall\LazyAccessListener">
247+
<tagname="monolog.logger"channel="security" />
248+
<argumenttype="service"id="security.token_storage" />
249+
<argumenttype="service"id="security.access.decision_manager" />
250+
<argumenttype="service"id="security.access_map" />
251+
<argumenttype="service"id="security.authentication.manager" />
252+
</service>
245253
</services>
246254
</container>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Bundle\SecurityBundle\Security;
13+
14+
useSymfony\Component\HttpKernel\Event\GetResponseEvent;
15+
useSymfony\Component\Security\Core\Exception\LazyResponseException;
16+
useSymfony\Component\Security\Http\Event\LazyResponseEvent;
17+
useSymfony\Component\Security\Http\Firewall\ExceptionListener;
18+
useSymfony\Component\Security\Http\Firewall\LazyAccessListener;
19+
useSymfony\Component\Security\Http\Firewall\ListenerInterface;
20+
useSymfony\Component\Security\Http\Firewall\LogoutListener;
21+
22+
/**
23+
* Lazily calls authentication listeners when actually required by the access listener.
24+
*
25+
* @author Nicolas Grekas <p@tchwork.com>
26+
*/
27+
class LazyFirewallContextextends FirewallContextimplements ListenerInterface
28+
{
29+
private$accessListener;
30+
31+
publicfunction__construct(iterable$listeners, ?ExceptionListener$exceptionListener, ?LogoutListener$logoutListener, ?FirewallConfig$config,LazyAccessListener$accessListener)
32+
{
33+
parent::__construct($listeners,$exceptionListener,$logoutListener,$config);
34+
35+
$this->accessListener =$accessListener;
36+
}
37+
38+
publicfunctiongetListeners():iterable
39+
{
40+
returnarray($this);
41+
}
42+
43+
publicfunctionhandle(GetResponseEvent$event)
44+
{
45+
$this->accessListener->getTokenStorage()->setInitializer(function ()use ($event) {
46+
$event =newLazyResponseEvent($event);
47+
foreach (parent::getListeners()as$listener) {
48+
$listener->handle($event);
49+
}
50+
});
51+
52+
try {
53+
$this->accessListener->handle($event);
54+
}catch (LazyResponseException$e) {
55+
$event->setResponse($e->getResponse());
56+
}
57+
}
58+
}

‎src/Symfony/Bundle/SecurityBundle/SecurityBundle.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
useSymfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass;
1616
useSymfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSessionDomainConstraintPass;
1717
useSymfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterCsrfTokenClearingLogoutHandlerPass;
18+
useSymfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterForAutoconfigurationPass;
1819
useSymfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FormLoginFactory;
1920
useSymfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FormLoginLdapFactory;
2021
useSymfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\GuardAuthenticationFactory;
@@ -64,5 +65,6 @@ public function build(ContainerBuilder $container)
6465
$container->addCompilerPass(newAddSecurityVotersPass());
6566
$container->addCompilerPass(newAddSessionDomainConstraintPass(), PassConfig::TYPE_BEFORE_REMOVING);
6667
$container->addCompilerPass(newRegisterCsrfTokenClearingLogoutHandlerPass());
68+
$container->addCompilerPass(newRegisterForAutoconfigurationPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION,200);
6769
}
6870
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Security\Core\Authentication\Token\Storage;
13+
14+
useSymfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
16+
/**
17+
* Lazily populates a token storage.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*
21+
* @final
22+
*/
23+
class LazyTokenStorageimplements TokenStorageInterface
24+
{
25+
private$storage;
26+
private$initializer;
27+
28+
publicfunction__construct(TokenStorageInterface$storage)
29+
{
30+
$this->storage =$storage;
31+
}
32+
33+
publicfunctionsetInitializer(\Closure$initializer)
34+
{
35+
$this->initializer =$initializer;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
publicfunctiongetToken()
42+
{
43+
if ($initializer =$this->initializer) {
44+
$this->initializer =null;
45+
$initializer();
46+
}
47+
48+
return$this->storage->getToken();
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
publicfunctionsetToken(TokenInterface$token =null)
55+
{
56+
$this->initializer =null;
57+
$this->storage->setToken($token);
58+
}
59+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Security\Core\Exception;
13+
14+
useSymfony\Component\HttpFoundation\Response;
15+
16+
/**
17+
* Wraps a lazily computed response in a signaling exception.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
class LazyResponseExceptionextends \Exceptionimplements ExceptionInterface
22+
{
23+
private$response;
24+
25+
publicfunction__construct(Response$response)
26+
{
27+
$this->response =$response;
28+
}
29+
30+
publicfunctiongetResponse():Response
31+
{
32+
return$this->response;
33+
}
34+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp