27
27
import org .springframework .core .ResolvableType ;
28
28
import org .springframework .security .access .hierarchicalroles .NullRoleHierarchy ;
29
29
import org .springframework .security .access .hierarchicalroles .RoleHierarchy ;
30
- import org .springframework .security .authorization .AuthenticatedAuthorizationManager ;
31
30
import org .springframework .security .authorization .AuthorityAuthorizationManager ;
32
31
import org .springframework .security .authorization .AuthorizationDecision ;
33
32
import org .springframework .security .authorization .AuthorizationEventPublisher ;
34
33
import org .springframework .security .authorization .AuthorizationManager ;
34
+ import org .springframework .security .authorization .AuthorizationManagerFactory ;
35
35
import org .springframework .security .authorization .AuthorizationManagers ;
36
- import org .springframework .security .authorization .SingleResultAuthorizationManager ;
37
36
import org .springframework .security .authorization .SpringAuthorizationEventPublisher ;
38
37
import org .springframework .security .config .ObjectPostProcessor ;
39
38
import org .springframework .security .config .annotation .web .AbstractRequestMatcherRegistry ;
@@ -62,9 +61,7 @@ public final class AuthorizeHttpRequestsConfigurer<H extends HttpSecurityBuilder
62
61
63
62
private final AuthorizationEventPublisher publisher ;
64
63
65
- private final Supplier <RoleHierarchy > roleHierarchy ;
66
-
67
- private String rolePrefix = "ROLE_" ;
64
+ private final AuthorizationManagerFactory <RequestAuthorizationContext > authorizationManagerFactory ;
68
65
69
66
private ObjectPostProcessor <AuthorizationManager <HttpServletRequest >> postProcessor = ObjectPostProcessor
70
67
.identity ();
@@ -81,20 +78,32 @@ public AuthorizeHttpRequestsConfigurer(ApplicationContext context) {
81
78
else {
82
79
this .publisher = new SpringAuthorizationEventPublisher (context );
83
80
}
84
- this .roleHierarchy = SingletonSupplier .of (() -> (context .getBeanNamesForType (RoleHierarchy .class ).length > 0 )
85
- ? context .getBean (RoleHierarchy .class ) : new NullRoleHierarchy ());
86
- String [] grantedAuthorityDefaultsBeanNames = context .getBeanNamesForType (GrantedAuthorityDefaults .class );
87
- if (grantedAuthorityDefaultsBeanNames .length > 0 ) {
88
- GrantedAuthorityDefaults grantedAuthorityDefaults = context .getBean (GrantedAuthorityDefaults .class );
89
- this .rolePrefix = grantedAuthorityDefaults .getRolePrefix ();
90
- }
81
+ this .authorizationManagerFactory = getAuthorizationManagerFactory (context );
91
82
ResolvableType type = ResolvableType .forClassWithGenerics (ObjectPostProcessor .class ,
92
83
ResolvableType .forClassWithGenerics (AuthorizationManager .class , HttpServletRequest .class ));
93
84
ObjectProvider <ObjectPostProcessor <AuthorizationManager <HttpServletRequest >>> provider = context
94
85
.getBeanProvider (type );
95
86
provider .ifUnique ((postProcessor ) -> this .postProcessor = postProcessor );
96
87
}
97
88
89
+ private AuthorizationManagerFactory <RequestAuthorizationContext > getAuthorizationManagerFactory (
90
+ ApplicationContext context ) {
91
+ ResolvableType authorizationManagerFactoryType = ResolvableType
92
+ .forClassWithGenerics (AuthorizationManagerFactory .class , RequestAuthorizationContext .class );
93
+ ObjectProvider <AuthorizationManagerFactory <RequestAuthorizationContext >> authorizationManagerFactoryProvider = context
94
+ .getBeanProvider (authorizationManagerFactoryType );
95
+
96
+ return authorizationManagerFactoryProvider .getIfAvailable (() -> {
97
+ Supplier <RoleHierarchy > roleHierarchy = SingletonSupplier
98
+ .of (() -> context .getBeanProvider (RoleHierarchy .class ).getIfAvailable (NullRoleHierarchy ::new ));
99
+ GrantedAuthorityDefaults grantedAuthorityDefaults = context .getBeanProvider (GrantedAuthorityDefaults .class )
100
+ .getIfAvailable ();
101
+ String rolePrefix = (grantedAuthorityDefaults != null ) ? grantedAuthorityDefaults .getRolePrefix () : "ROLE_" ;
102
+
103
+ return new RequestAuthorizationContextAuthorizationManagerFactory (roleHierarchy , rolePrefix );
104
+ });
105
+ }
106
+
98
107
/**
99
108
* The {@link AuthorizationManagerRequestMatcherRegistry} is what users will interact
100
109
* with after applying the {@link AuthorizeHttpRequestsConfigurer}.
@@ -173,7 +182,7 @@ private AuthorizationManager<HttpServletRequest> createAuthorizationManager() {
173
182
@ Override
174
183
protected AuthorizedUrl chainRequestMatchers (List <RequestMatcher > requestMatchers ) {
175
184
this .unmappedMatchers = requestMatchers ;
176
- return new AuthorizedUrl (requestMatchers );
185
+ return new AuthorizedUrl (requestMatchers , AuthorizeHttpRequestsConfigurer . this . authorizationManagerFactory );
177
186
}
178
187
179
188
/**
@@ -201,20 +210,31 @@ public class AuthorizedUrl {
201
210
202
211
private final List <? extends RequestMatcher > matchers ;
203
212
213
+ private AuthorizationManagerFactory <RequestAuthorizationContext > authorizationManagerFactory ;
214
+
204
215
private boolean not ;
205
216
206
217
/**
207
218
* Creates an instance.
208
219
* @param matchers the {@link RequestMatcher} instances to map
220
+ * @param authorizationManagerFactory the {@link AuthorizationManagerFactory} for
221
+ * creating instances of {@link AuthorizationManager}
209
222
*/
210
- AuthorizedUrl (List <? extends RequestMatcher > matchers ) {
223
+ AuthorizedUrl (List <? extends RequestMatcher > matchers ,
224
+ AuthorizationManagerFactory <RequestAuthorizationContext > authorizationManagerFactory ) {
211
225
this .matchers = matchers ;
226
+ this .authorizationManagerFactory = authorizationManagerFactory ;
212
227
}
213
228
214
229
protected List <? extends RequestMatcher > getMatchers () {
215
230
return this .matchers ;
216
231
}
217
232
233
+ void setAuthorizationManagerFactory (
234
+ AuthorizationManagerFactory <RequestAuthorizationContext > authorizationManagerFactory ) {
235
+ this .authorizationManagerFactory = authorizationManagerFactory ;
236
+ }
237
+
218
238
/**
219
239
* Negates the following authorization rule.
220
240
* @return the {@link AuthorizedUrl} for further customization
@@ -231,7 +251,7 @@ public AuthorizedUrl not() {
231
251
* customizations
232
252
*/
233
253
public AuthorizationManagerRequestMatcherRegistry permitAll () {
234
- return access (SingleResultAuthorizationManager .permitAll ());
254
+ return access (this . authorizationManagerFactory .permitAll ());
235
255
}
236
256
237
257
/**
@@ -240,7 +260,7 @@ public AuthorizationManagerRequestMatcherRegistry permitAll() {
240
260
* customizations
241
261
*/
242
262
public AuthorizationManagerRequestMatcherRegistry denyAll () {
243
- return access (SingleResultAuthorizationManager .denyAll ());
263
+ return access (this . authorizationManagerFactory .denyAll ());
244
264
}
245
265
246
266
/**
@@ -251,8 +271,7 @@ public AuthorizationManagerRequestMatcherRegistry denyAll() {
251
271
* customizations
252
272
*/
253
273
public AuthorizationManagerRequestMatcherRegistry hasRole (String role ) {
254
- return access (withRoleHierarchy (AuthorityAuthorizationManager
255
- .hasAnyRole (AuthorizeHttpRequestsConfigurer .this .rolePrefix , new String [] { role })));
274
+ return access (this .authorizationManagerFactory .hasRole (role ));
256
275
}
257
276
258
277
/**
@@ -264,8 +283,7 @@ public AuthorizationManagerRequestMatcherRegistry hasRole(String role) {
264
283
* customizations
265
284
*/
266
285
public AuthorizationManagerRequestMatcherRegistry hasAnyRole (String ... roles ) {
267
- return access (withRoleHierarchy (
268
- AuthorityAuthorizationManager .hasAnyRole (AuthorizeHttpRequestsConfigurer .this .rolePrefix , roles )));
286
+ return access (this .authorizationManagerFactory .hasAnyRole (roles ));
269
287
}
270
288
271
289
/**
@@ -275,7 +293,7 @@ public AuthorizationManagerRequestMatcherRegistry hasAnyRole(String... roles) {
275
293
* customizations
276
294
*/
277
295
public AuthorizationManagerRequestMatcherRegistry hasAuthority (String authority ) {
278
- return access (withRoleHierarchy ( AuthorityAuthorizationManager . hasAuthority (authority ) ));
296
+ return access (this . authorizationManagerFactory . hasAuthority (authority ));
279
297
}
280
298
281
299
/**
@@ -286,13 +304,7 @@ public AuthorizationManagerRequestMatcherRegistry hasAuthority(String authority)
286
304
* customizations
287
305
*/
288
306
public AuthorizationManagerRequestMatcherRegistry hasAnyAuthority (String ... authorities ) {
289
- return access (withRoleHierarchy (AuthorityAuthorizationManager .hasAnyAuthority (authorities )));
290
- }
291
-
292
- private AuthorityAuthorizationManager <RequestAuthorizationContext > withRoleHierarchy (
293
- AuthorityAuthorizationManager <RequestAuthorizationContext > manager ) {
294
- manager .setRoleHierarchy (AuthorizeHttpRequestsConfigurer .this .roleHierarchy .get ());
295
- return manager ;
307
+ return access (this .authorizationManagerFactory .hasAnyAuthority (authorities ));
296
308
}
297
309
298
310
/**
@@ -301,7 +313,7 @@ private AuthorityAuthorizationManager<RequestAuthorizationContext> withRoleHiera
301
313
* customizations
302
314
*/
303
315
public AuthorizationManagerRequestMatcherRegistry authenticated () {
304
- return access (AuthenticatedAuthorizationManager .authenticated ());
316
+ return access (this . authorizationManagerFactory .authenticated ());
305
317
}
306
318
307
319
/**
@@ -313,7 +325,7 @@ public AuthorizationManagerRequestMatcherRegistry authenticated() {
313
325
* @see RememberMeConfigurer
314
326
*/
315
327
public AuthorizationManagerRequestMatcherRegistry fullyAuthenticated () {
316
- return access (AuthenticatedAuthorizationManager .fullyAuthenticated ());
328
+ return access (this . authorizationManagerFactory .fullyAuthenticated ());
317
329
}
318
330
319
331
/**
@@ -324,7 +336,7 @@ public AuthorizationManagerRequestMatcherRegistry fullyAuthenticated() {
324
336
* @see RememberMeConfigurer
325
337
*/
326
338
public AuthorizationManagerRequestMatcherRegistry rememberMe () {
327
- return access (AuthenticatedAuthorizationManager .rememberMe ());
339
+ return access (this . authorizationManagerFactory .rememberMe ());
328
340
}
329
341
330
342
/**
@@ -334,7 +346,7 @@ public AuthorizationManagerRequestMatcherRegistry rememberMe() {
334
346
* @since 5.8
335
347
*/
336
348
public AuthorizationManagerRequestMatcherRegistry anonymous () {
337
- return access (AuthenticatedAuthorizationManager .anonymous ());
349
+ return access (this . authorizationManagerFactory .anonymous ());
338
350
}
339
351
340
352
/**
@@ -403,4 +415,45 @@ public AuthorizationManagerRequestMatcherRegistry equalTo(Function<Authenticatio
403
415
404
416
}
405
417
418
+ static final class RequestAuthorizationContextAuthorizationManagerFactory
419
+ implements AuthorizationManagerFactory <RequestAuthorizationContext > {
420
+
421
+ private final Supplier <RoleHierarchy > roleHierarchy ;
422
+
423
+ private final String rolePrefix ;
424
+
425
+ RequestAuthorizationContextAuthorizationManagerFactory (Supplier <RoleHierarchy > roleHierarchy ,
426
+ String rolePrefix ) {
427
+ this .roleHierarchy = roleHierarchy ;
428
+ this .rolePrefix = rolePrefix ;
429
+ }
430
+
431
+ @ Override
432
+ public AuthorizationManager <RequestAuthorizationContext > hasRole (String role ) {
433
+ return withRoleHierarchy (AuthorityAuthorizationManager .hasAnyRole (this .rolePrefix , new String [] { role }));
434
+ }
435
+
436
+ @ Override
437
+ public AuthorizationManager <RequestAuthorizationContext > hasAnyRole (String ... roles ) {
438
+ return withRoleHierarchy (AuthorityAuthorizationManager .hasAnyRole (this .rolePrefix , roles ));
439
+ }
440
+
441
+ @ Override
442
+ public AuthorizationManager <RequestAuthorizationContext > hasAuthority (String authority ) {
443
+ return withRoleHierarchy (AuthorityAuthorizationManager .hasAuthority (authority ));
444
+ }
445
+
446
+ @ Override
447
+ public AuthorizationManager <RequestAuthorizationContext > hasAnyAuthority (String ... authorities ) {
448
+ return withRoleHierarchy (AuthorityAuthorizationManager .hasAnyAuthority (authorities ));
449
+ }
450
+
451
+ private AuthorityAuthorizationManager <RequestAuthorizationContext > withRoleHierarchy (
452
+ AuthorityAuthorizationManager <RequestAuthorizationContext > manager ) {
453
+ manager .setRoleHierarchy (this .roleHierarchy .get ());
454
+ return manager ;
455
+ }
456
+
457
+ }
458
+
406
459
}
0 commit comments