@@ -116,3 +116,138 @@ is the service id of your user checker:
116
116
// ...
117
117
;
118
118
};
119
+
120
+ Using Multiple User Checkers
121
+ ----------------------------
122
+
123
+ .. versionadded :: 6.2
124
+
125
+ The ``ChainUserChecker `` class was added in Symfony 6.2.
126
+
127
+ It is common for applications to have multiple authentication entry points (such as
128
+ traditional form based login and an API) which may have unique checker rules for each
129
+ entry point as well as common rules for all entry points. To allow using multiple user
130
+ checkers on a firewall, a service for the :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ ChainUserChecker `
131
+ class is created for each firewall.
132
+
133
+ To use the chain user checker, you will need to tag your user checker services with the
134
+ ``security.user_checker.<firewall> `` tag (where ``<firewall> `` is the name of the firewall
135
+ in your security configuration) and configure your firewall to use the ``security.user_checker.chain.<firewall> ``
136
+ service. The service tag also supports the priority attribute, allowing you to define the
137
+ order in which user checkers are called.
138
+
139
+ .. configuration-block ::
140
+
141
+ .. code-block :: yaml
142
+
143
+ # config/services.yaml
144
+
145
+ # ...
146
+ services :
147
+ App\Security\AccountEnabledUserChecker :
148
+ tags :
149
+ - { name: security.user_checker.api, priority: 10 }
150
+ - { name: security.user_checker.main, priority: 10 }
151
+
152
+ App\Security\APIAccessAllowedUserChecker :
153
+ tags :
154
+ - { name: security.user_checker.api, priority: 5 }
155
+
156
+ # config/packages/security.yaml
157
+
158
+ # ...
159
+ security :
160
+ firewalls :
161
+ api :
162
+ pattern : ^/api
163
+ user_checker : security.user_checker.chain.api
164
+ # ...
165
+ main :
166
+ pattern : ^/
167
+ user_checker : security.user_checker.chain.main
168
+ # ...
169
+
170
+ .. code-block :: xml
171
+
172
+ <!-- config/services.xml -->
173
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
174
+ <container xmlns =" http://symfony.com/schema/dic/services"
175
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
176
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
177
+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
178
+
179
+ <services >
180
+ <!-- ... -->
181
+
182
+ <service id =" App\Security\AccountEnabledUserChecker" >
183
+ <tag name =" security.user_checker.api" priority =" 10" />
184
+ <tag name =" security.user_checker.main" priority =" 10" />
185
+ </service >
186
+
187
+ <service id =" App\Security\APIAccessAllowedUserChecker" >
188
+ <tag name =" security.user_checker.api" priority =" 5" />
189
+ </service >
190
+ </services >
191
+ </container >
192
+
193
+ <!-- config/packages/security.xml -->
194
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
195
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
196
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
197
+ xmlns : srv =" http://symfony.com/schema/dic/services"
198
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
199
+ https://symfony.com/schema/dic/services/services-1.0.xsd
200
+ http://symfony.com/schema/dic/security
201
+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
202
+
203
+ <config >
204
+ <!-- ... -->
205
+ <firewall name =" api"
206
+ pattern =" ^/api"
207
+ user-checker =" security.user_checker.chain.api" >
208
+ <!-- ... -->
209
+ </firewall >
210
+ <firewall name =" main"
211
+ pattern =" ^/"
212
+ user-checker =" security.user_checker.chain.main" >
213
+ <!-- ... -->
214
+ </firewall >
215
+ </config >
216
+ </srv : container >
217
+
218
+ .. code-block :: php
219
+
220
+ // config/services.php
221
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
222
+
223
+ use App\Security\AccountEnabledUserChecker;
224
+ use App\Security\APIAccessAllowedUserChecker;
225
+
226
+ return function(ContainerConfigurator $configurator) {
227
+ $services = $configurator->services();
228
+
229
+ $services->set(AccountEnabledUserChecker::class)
230
+ ->tag('security.user_checker.api', ['priority' => 10])
231
+ ->tag('security.user_checker.main', ['priority' => 10]);
232
+
233
+ $services->set(APIAccessAllowedUserChecker::class)
234
+ ->tag('security.user_checker.api', ['priority' => 5]);
235
+ };
236
+
237
+ // config/packages/security.php
238
+ use Symfony\Config\SecurityConfig;
239
+
240
+ return static function (SecurityConfig $security) {
241
+ // ...
242
+ $security->firewall('api')
243
+ ->pattern('^/api')
244
+ ->userChecker('security.user_checker.chain.api')
245
+ // ...
246
+ ;
247
+
248
+ $security->firewall('main')
249
+ ->pattern('^/')
250
+ ->userChecker('security.user_checker.chain.main')
251
+ // ...
252
+ ;
253
+ };
0 commit comments