Skip to content

Commit 96ebab3

Browse files
committed
Remove Type Parameter
Closes gh-14012
1 parent 82e11cc commit 96ebab3

File tree

1 file changed

+164
-23
lines changed

1 file changed

+164
-23
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/AuthorizeHttpRequestsConfigurer.java

Lines changed: 164 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private ObservationRegistry getObservationRegistry() {
146146
* @author Evgeniy Cheban
147147
*/
148148
public final class AuthorizationManagerRequestMatcherRegistry
149-
extends AbstractRequestMatcherBuilderRegistry<AuthorizedUrl<AuthorizationManagerRequestMatcherRegistry>> {
149+
extends AbstractRequestMatcherBuilderRegistry<AuthorizedUrl> {
150150

151151
private final RequestMatcherDelegatingAuthorizationManager.Builder managerBuilder = RequestMatcherDelegatingAuthorizationManager
152152
.builder();
@@ -209,10 +209,9 @@ private AuthorizationManager<HttpServletRequest> createAuthorizationManager() {
209209
}
210210

211211
@Override
212-
protected AuthorizedUrl<AuthorizationManagerRequestMatcherRegistry> chainRequestMatchers(
213-
List<RequestMatcher> requestMatchers) {
212+
protected AuthorizedUrl chainRequestMatchers(List<RequestMatcher> requestMatchers) {
214213
this.unmappedMatchers = requestMatchers;
215-
return new AuthorizedUrl<>(
214+
return new AuthorizedUrl(
216215
(manager) -> AuthorizeHttpRequestsConfigurer.this.addMapping(requestMatchers, manager));
217216
}
218217

@@ -416,8 +415,8 @@ public H and() {
416415
* @see AbstractRequestMatcherRegistry
417416
* @see AuthorizeHttpRequestsConfigurer
418417
*/
419-
public final class AuthorizationManagerServletRequestMatcherRegistry extends
420-
AbstractRequestMatcherBuilderRegistry<AuthorizedUrl<AuthorizationManagerServletRequestMatcherRegistry>> {
418+
public final class AuthorizationManagerServletRequestMatcherRegistry
419+
extends AbstractRequestMatcherBuilderRegistry<ServletAuthorizedUrl> {
421420

422421
private final RequestMatcherDelegatingAuthorizationManager.Builder managerBuilder = RequestMatcherDelegatingAuthorizationManager
423422
.builder();
@@ -437,10 +436,9 @@ AuthorizationManager<RequestAuthorizationContext> authorizationManager() {
437436
}
438437

439438
@Override
440-
protected AuthorizedUrl<AuthorizationManagerServletRequestMatcherRegistry> chainRequestMatchers(
441-
List<RequestMatcher> requestMatchers) {
439+
protected ServletAuthorizedUrl chainRequestMatchers(List<RequestMatcher> requestMatchers) {
442440
this.unmappedMatchers = requestMatchers;
443-
return new AuthorizedUrl<>((manager) -> addMapping(requestMatchers, manager));
441+
return new ServletAuthorizedUrl((manager) -> addMapping(requestMatchers, manager));
444442
}
445443

446444
private AuthorizationManagerServletRequestMatcherRegistry addMapping(List<RequestMatcher> matchers,
@@ -454,6 +452,147 @@ private AuthorizationManagerServletRequestMatcherRegistry addMapping(List<Reques
454452

455453
}
456454

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+
457596
}
458597

459598
/**
@@ -462,11 +601,12 @@ private AuthorizationManagerServletRequestMatcherRegistry addMapping(List<Reques
462601
*
463602
* @author Evgeniy Cheban
464603
*/
465-
public class AuthorizedUrl<R> {
604+
public class AuthorizedUrl {
466605

467-
private final Function<AuthorizationManager<RequestAuthorizationContext>, R> registrar;
606+
private final Function<AuthorizationManager<RequestAuthorizationContext>, AuthorizationManagerRequestMatcherRegistry> registrar;
468607

469-
AuthorizedUrl(Function<AuthorizationManager<RequestAuthorizationContext>, R> registrar) {
608+
AuthorizedUrl(
609+
Function<AuthorizationManager<RequestAuthorizationContext>, AuthorizationManagerRequestMatcherRegistry> registrar) {
470610
this.registrar = registrar;
471611
}
472612

@@ -475,7 +615,7 @@ public class AuthorizedUrl<R> {
475615
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
476616
* customizations
477617
*/
478-
public R permitAll() {
618+
public AuthorizationManagerRequestMatcherRegistry permitAll() {
479619
return access(permitAllAuthorizationManager);
480620
}
481621

@@ -484,7 +624,7 @@ public R permitAll() {
484624
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
485625
* customizations
486626
*/
487-
public R denyAll() {
627+
public AuthorizationManagerRequestMatcherRegistry denyAll() {
488628
return access((a, o) -> new AuthorizationDecision(false));
489629
}
490630

@@ -495,7 +635,7 @@ public R denyAll() {
495635
* @return {@link AuthorizationManagerRequestMatcherRegistry} for further
496636
* customizations
497637
*/
498-
public R hasRole(String role) {
638+
public AuthorizationManagerRequestMatcherRegistry hasRole(String role) {
499639
return access(withRoleHierarchy(AuthorityAuthorizationManager
500640
.hasAnyRole(AuthorizeHttpRequestsConfigurer.this.rolePrefix, new String[] { role })));
501641
}
@@ -508,7 +648,7 @@ public R hasRole(String role) {
508648
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
509649
* customizations
510650
*/
511-
public R hasAnyRole(String... roles) {
651+
public AuthorizationManagerRequestMatcherRegistry hasAnyRole(String... roles) {
512652
return access(withRoleHierarchy(
513653
AuthorityAuthorizationManager.hasAnyRole(AuthorizeHttpRequestsConfigurer.this.rolePrefix, roles)));
514654
}
@@ -519,7 +659,7 @@ public R hasAnyRole(String... roles) {
519659
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
520660
* customizations
521661
*/
522-
public R hasAuthority(String authority) {
662+
public AuthorizationManagerRequestMatcherRegistry hasAuthority(String authority) {
523663
return access(withRoleHierarchy(AuthorityAuthorizationManager.hasAuthority(authority)));
524664
}
525665

@@ -530,7 +670,7 @@ public R hasAuthority(String authority) {
530670
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
531671
* customizations
532672
*/
533-
public R hasAnyAuthority(String... authorities) {
673+
public AuthorizationManagerRequestMatcherRegistry hasAnyAuthority(String... authorities) {
534674
return access(withRoleHierarchy(AuthorityAuthorizationManager.hasAnyAuthority(authorities)));
535675
}
536676

@@ -545,7 +685,7 @@ private AuthorityAuthorizationManager<RequestAuthorizationContext> withRoleHiera
545685
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
546686
* customizations
547687
*/
548-
public R authenticated() {
688+
public AuthorizationManagerRequestMatcherRegistry authenticated() {
549689
return access(AuthenticatedAuthorizationManager.authenticated());
550690
}
551691

@@ -557,7 +697,7 @@ public R authenticated() {
557697
* @since 5.8
558698
* @see RememberMeConfigurer
559699
*/
560-
public R fullyAuthenticated() {
700+
public AuthorizationManagerRequestMatcherRegistry fullyAuthenticated() {
561701
return access(AuthenticatedAuthorizationManager.fullyAuthenticated());
562702
}
563703

@@ -568,7 +708,7 @@ public R fullyAuthenticated() {
568708
* @since 5.8
569709
* @see RememberMeConfigurer
570710
*/
571-
public R rememberMe() {
711+
public AuthorizationManagerRequestMatcherRegistry rememberMe() {
572712
return access(AuthenticatedAuthorizationManager.rememberMe());
573713
}
574714

@@ -578,7 +718,7 @@ public R rememberMe() {
578718
* customization
579719
* @since 5.8
580720
*/
581-
public R anonymous() {
721+
public AuthorizationManagerRequestMatcherRegistry anonymous() {
582722
return access(AuthenticatedAuthorizationManager.anonymous());
583723
}
584724

@@ -588,7 +728,8 @@ public R anonymous() {
588728
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
589729
* customizations
590730
*/
591-
public R access(AuthorizationManager<RequestAuthorizationContext> manager) {
731+
public AuthorizationManagerRequestMatcherRegistry access(
732+
AuthorizationManager<RequestAuthorizationContext> manager) {
592733
Assert.notNull(manager, "manager cannot be null");
593734
return this.registrar.apply(manager);
594735
}

0 commit comments

Comments
 (0)