@@ -146,7 +146,7 @@ private ObservationRegistry getObservationRegistry() {
146
146
* @author Evgeniy Cheban
147
147
*/
148
148
public final class AuthorizationManagerRequestMatcherRegistry
149
- extends AbstractRequestMatcherBuilderRegistry <AuthorizedUrl < AuthorizationManagerRequestMatcherRegistry > > {
149
+ extends AbstractRequestMatcherBuilderRegistry <AuthorizedUrl > {
150
150
151
151
private final RequestMatcherDelegatingAuthorizationManager .Builder managerBuilder = RequestMatcherDelegatingAuthorizationManager
152
152
.builder ();
@@ -209,10 +209,9 @@ private AuthorizationManager<HttpServletRequest> createAuthorizationManager() {
209
209
}
210
210
211
211
@ Override
212
- protected AuthorizedUrl <AuthorizationManagerRequestMatcherRegistry > chainRequestMatchers (
213
- List <RequestMatcher > requestMatchers ) {
212
+ protected AuthorizedUrl chainRequestMatchers (List <RequestMatcher > requestMatchers ) {
214
213
this .unmappedMatchers = requestMatchers ;
215
- return new AuthorizedUrl <> (
214
+ return new AuthorizedUrl (
216
215
(manager ) -> AuthorizeHttpRequestsConfigurer .this .addMapping (requestMatchers , manager ));
217
216
}
218
217
@@ -416,8 +415,8 @@ public H and() {
416
415
* @see AbstractRequestMatcherRegistry
417
416
* @see AuthorizeHttpRequestsConfigurer
418
417
*/
419
- public final class AuthorizationManagerServletRequestMatcherRegistry extends
420
- AbstractRequestMatcherBuilderRegistry <AuthorizedUrl < AuthorizationManagerServletRequestMatcherRegistry > > {
418
+ public final class AuthorizationManagerServletRequestMatcherRegistry
419
+ extends AbstractRequestMatcherBuilderRegistry <ServletAuthorizedUrl > {
421
420
422
421
private final RequestMatcherDelegatingAuthorizationManager .Builder managerBuilder = RequestMatcherDelegatingAuthorizationManager
423
422
.builder ();
@@ -437,10 +436,9 @@ AuthorizationManager<RequestAuthorizationContext> authorizationManager() {
437
436
}
438
437
439
438
@ Override
440
- protected AuthorizedUrl <AuthorizationManagerServletRequestMatcherRegistry > chainRequestMatchers (
441
- List <RequestMatcher > requestMatchers ) {
439
+ protected ServletAuthorizedUrl chainRequestMatchers (List <RequestMatcher > requestMatchers ) {
442
440
this .unmappedMatchers = requestMatchers ;
443
- return new AuthorizedUrl <> ((manager ) -> addMapping (requestMatchers , manager ));
441
+ return new ServletAuthorizedUrl ((manager ) -> addMapping (requestMatchers , manager ));
444
442
}
445
443
446
444
private AuthorizationManagerServletRequestMatcherRegistry addMapping (List <RequestMatcher > matchers ,
@@ -454,6 +452,147 @@ private AuthorizationManagerServletRequestMatcherRegistry addMapping(List<Reques
454
452
455
453
}
456
454
455
+ /**
456
+ * An object that allows configuring the {@link AuthorizationManager} for
457
+ * {@link RequestMatcher}s.
458
+ *
459
+ * @author Josh Cummings
460
+ * @since 6.2
461
+ */
462
+ public final class ServletAuthorizedUrl {
463
+
464
+ private final Function <AuthorizationManager <RequestAuthorizationContext >, AuthorizationManagerServletRequestMatcherRegistry > registrar ;
465
+
466
+ ServletAuthorizedUrl (
467
+ Function <AuthorizationManager <RequestAuthorizationContext >, AuthorizationManagerServletRequestMatcherRegistry > registrar ) {
468
+ this .registrar = registrar ;
469
+ }
470
+
471
+ /**
472
+ * Specify that URLs are allowed by anyone.
473
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
474
+ * customizations
475
+ */
476
+ public AuthorizationManagerServletRequestMatcherRegistry permitAll () {
477
+ return access (permitAllAuthorizationManager );
478
+ }
479
+
480
+ /**
481
+ * Specify that URLs are not allowed by anyone.
482
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
483
+ * customizations
484
+ */
485
+ public AuthorizationManagerServletRequestMatcherRegistry denyAll () {
486
+ return access ((a , o ) -> new AuthorizationDecision (false ));
487
+ }
488
+
489
+ /**
490
+ * Specifies a user requires a role.
491
+ * @param role the role that should be required which is prepended with ROLE_
492
+ * automatically (i.e. USER, ADMIN, etc). It should not start with ROLE_
493
+ * @return {@link AuthorizationManagerRequestMatcherRegistry} for further
494
+ * customizations
495
+ */
496
+ public AuthorizationManagerServletRequestMatcherRegistry hasRole (String role ) {
497
+ return access (withRoleHierarchy (AuthorityAuthorizationManager
498
+ .hasAnyRole (AuthorizeHttpRequestsConfigurer .this .rolePrefix , new String [] { role })));
499
+ }
500
+
501
+ /**
502
+ * Specifies that a user requires one of many roles.
503
+ * @param roles the roles that the user should have at least one of (i.e.
504
+ * ADMIN, USER, etc). Each role should not start with ROLE_ since it is
505
+ * automatically prepended already
506
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
507
+ * customizations
508
+ */
509
+ public AuthorizationManagerServletRequestMatcherRegistry hasAnyRole (String ... roles ) {
510
+ return access (withRoleHierarchy (AuthorityAuthorizationManager
511
+ .hasAnyRole (AuthorizeHttpRequestsConfigurer .this .rolePrefix , roles )));
512
+ }
513
+
514
+ /**
515
+ * Specifies a user requires an authority.
516
+ * @param authority the authority that should be required
517
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
518
+ * customizations
519
+ */
520
+ public AuthorizationManagerServletRequestMatcherRegistry hasAuthority (String authority ) {
521
+ return access (withRoleHierarchy (AuthorityAuthorizationManager .hasAuthority (authority )));
522
+ }
523
+
524
+ /**
525
+ * Specifies that a user requires one of many authorities.
526
+ * @param authorities the authorities that the user should have at least one
527
+ * of (i.e. ROLE_USER, ROLE_ADMIN, etc)
528
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
529
+ * customizations
530
+ */
531
+ public AuthorizationManagerServletRequestMatcherRegistry hasAnyAuthority (String ... authorities ) {
532
+ return access (withRoleHierarchy (AuthorityAuthorizationManager .hasAnyAuthority (authorities )));
533
+ }
534
+
535
+ private AuthorityAuthorizationManager <RequestAuthorizationContext > withRoleHierarchy (
536
+ AuthorityAuthorizationManager <RequestAuthorizationContext > manager ) {
537
+ manager .setRoleHierarchy (AuthorizeHttpRequestsConfigurer .this .roleHierarchy .get ());
538
+ return manager ;
539
+ }
540
+
541
+ /**
542
+ * Specify that URLs are allowed by any authenticated user.
543
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
544
+ * customizations
545
+ */
546
+ public AuthorizationManagerServletRequestMatcherRegistry authenticated () {
547
+ return access (AuthenticatedAuthorizationManager .authenticated ());
548
+ }
549
+
550
+ /**
551
+ * Specify that URLs are allowed by users who have authenticated and were not
552
+ * "remembered".
553
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
554
+ * customization
555
+ * @see RememberMeConfigurer
556
+ */
557
+ public AuthorizationManagerServletRequestMatcherRegistry fullyAuthenticated () {
558
+ return access (AuthenticatedAuthorizationManager .fullyAuthenticated ());
559
+ }
560
+
561
+ /**
562
+ * Specify that URLs are allowed by users that have been remembered.
563
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
564
+ * customization
565
+ * @since 5.8
566
+ * @see RememberMeConfigurer
567
+ */
568
+ public AuthorizationManagerServletRequestMatcherRegistry rememberMe () {
569
+ return access (AuthenticatedAuthorizationManager .rememberMe ());
570
+ }
571
+
572
+ /**
573
+ * Specify that URLs are allowed by anonymous users.
574
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
575
+ * customization
576
+ * @since 5.8
577
+ */
578
+ public AuthorizationManagerServletRequestMatcherRegistry anonymous () {
579
+ return access (AuthenticatedAuthorizationManager .anonymous ());
580
+ }
581
+
582
+ /**
583
+ * Allows specifying a custom {@link AuthorizationManager}.
584
+ * @param manager the {@link AuthorizationManager} to use
585
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
586
+ * customizations
587
+ */
588
+ public AuthorizationManagerServletRequestMatcherRegistry access (
589
+ AuthorizationManager <RequestAuthorizationContext > manager ) {
590
+ Assert .notNull (manager , "manager cannot be null" );
591
+ return this .registrar .apply (manager );
592
+ }
593
+
594
+ }
595
+
457
596
}
458
597
459
598
/**
@@ -462,11 +601,12 @@ private AuthorizationManagerServletRequestMatcherRegistry addMapping(List<Reques
462
601
*
463
602
* @author Evgeniy Cheban
464
603
*/
465
- public class AuthorizedUrl < R > {
604
+ public class AuthorizedUrl {
466
605
467
- private final Function <AuthorizationManager <RequestAuthorizationContext >, R > registrar ;
606
+ private final Function <AuthorizationManager <RequestAuthorizationContext >, AuthorizationManagerRequestMatcherRegistry > registrar ;
468
607
469
- AuthorizedUrl (Function <AuthorizationManager <RequestAuthorizationContext >, R > registrar ) {
608
+ AuthorizedUrl (
609
+ Function <AuthorizationManager <RequestAuthorizationContext >, AuthorizationManagerRequestMatcherRegistry > registrar ) {
470
610
this .registrar = registrar ;
471
611
}
472
612
@@ -475,7 +615,7 @@ public class AuthorizedUrl<R> {
475
615
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
476
616
* customizations
477
617
*/
478
- public R permitAll () {
618
+ public AuthorizationManagerRequestMatcherRegistry permitAll () {
479
619
return access (permitAllAuthorizationManager );
480
620
}
481
621
@@ -484,7 +624,7 @@ public R permitAll() {
484
624
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
485
625
* customizations
486
626
*/
487
- public R denyAll () {
627
+ public AuthorizationManagerRequestMatcherRegistry denyAll () {
488
628
return access ((a , o ) -> new AuthorizationDecision (false ));
489
629
}
490
630
@@ -495,7 +635,7 @@ public R denyAll() {
495
635
* @return {@link AuthorizationManagerRequestMatcherRegistry} for further
496
636
* customizations
497
637
*/
498
- public R hasRole (String role ) {
638
+ public AuthorizationManagerRequestMatcherRegistry hasRole (String role ) {
499
639
return access (withRoleHierarchy (AuthorityAuthorizationManager
500
640
.hasAnyRole (AuthorizeHttpRequestsConfigurer .this .rolePrefix , new String [] { role })));
501
641
}
@@ -508,7 +648,7 @@ public R hasRole(String role) {
508
648
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
509
649
* customizations
510
650
*/
511
- public R hasAnyRole (String ... roles ) {
651
+ public AuthorizationManagerRequestMatcherRegistry hasAnyRole (String ... roles ) {
512
652
return access (withRoleHierarchy (
513
653
AuthorityAuthorizationManager .hasAnyRole (AuthorizeHttpRequestsConfigurer .this .rolePrefix , roles )));
514
654
}
@@ -519,7 +659,7 @@ public R hasAnyRole(String... roles) {
519
659
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
520
660
* customizations
521
661
*/
522
- public R hasAuthority (String authority ) {
662
+ public AuthorizationManagerRequestMatcherRegistry hasAuthority (String authority ) {
523
663
return access (withRoleHierarchy (AuthorityAuthorizationManager .hasAuthority (authority )));
524
664
}
525
665
@@ -530,7 +670,7 @@ public R hasAuthority(String authority) {
530
670
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
531
671
* customizations
532
672
*/
533
- public R hasAnyAuthority (String ... authorities ) {
673
+ public AuthorizationManagerRequestMatcherRegistry hasAnyAuthority (String ... authorities ) {
534
674
return access (withRoleHierarchy (AuthorityAuthorizationManager .hasAnyAuthority (authorities )));
535
675
}
536
676
@@ -545,7 +685,7 @@ private AuthorityAuthorizationManager<RequestAuthorizationContext> withRoleHiera
545
685
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
546
686
* customizations
547
687
*/
548
- public R authenticated () {
688
+ public AuthorizationManagerRequestMatcherRegistry authenticated () {
549
689
return access (AuthenticatedAuthorizationManager .authenticated ());
550
690
}
551
691
@@ -557,7 +697,7 @@ public R authenticated() {
557
697
* @since 5.8
558
698
* @see RememberMeConfigurer
559
699
*/
560
- public R fullyAuthenticated () {
700
+ public AuthorizationManagerRequestMatcherRegistry fullyAuthenticated () {
561
701
return access (AuthenticatedAuthorizationManager .fullyAuthenticated ());
562
702
}
563
703
@@ -568,7 +708,7 @@ public R fullyAuthenticated() {
568
708
* @since 5.8
569
709
* @see RememberMeConfigurer
570
710
*/
571
- public R rememberMe () {
711
+ public AuthorizationManagerRequestMatcherRegistry rememberMe () {
572
712
return access (AuthenticatedAuthorizationManager .rememberMe ());
573
713
}
574
714
@@ -578,7 +718,7 @@ public R rememberMe() {
578
718
* customization
579
719
* @since 5.8
580
720
*/
581
- public R anonymous () {
721
+ public AuthorizationManagerRequestMatcherRegistry anonymous () {
582
722
return access (AuthenticatedAuthorizationManager .anonymous ());
583
723
}
584
724
@@ -588,7 +728,8 @@ public R anonymous() {
588
728
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
589
729
* customizations
590
730
*/
591
- public R access (AuthorizationManager <RequestAuthorizationContext > manager ) {
731
+ public AuthorizationManagerRequestMatcherRegistry access (
732
+ AuthorizationManager <RequestAuthorizationContext > manager ) {
592
733
Assert .notNull (manager , "manager cannot be null" );
593
734
return this .registrar .apply (manager );
594
735
}
0 commit comments