Skip to content

Commit

Permalink
Remove Type Parameter
Browse files Browse the repository at this point in the history
Closes gh-14012
  • Loading branch information
jzheaux committed Oct 14, 2023
1 parent 82e11cc commit 96ebab3
Showing 1 changed file with 164 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private ObservationRegistry getObservationRegistry() {
* @author Evgeniy Cheban
*/
public final class AuthorizationManagerRequestMatcherRegistry
extends AbstractRequestMatcherBuilderRegistry<AuthorizedUrl<AuthorizationManagerRequestMatcherRegistry>> {
extends AbstractRequestMatcherBuilderRegistry<AuthorizedUrl> {

private final RequestMatcherDelegatingAuthorizationManager.Builder managerBuilder = RequestMatcherDelegatingAuthorizationManager
.builder();
Expand Down Expand Up @@ -209,10 +209,9 @@ private AuthorizationManager<HttpServletRequest> createAuthorizationManager() {
}

@Override
protected AuthorizedUrl<AuthorizationManagerRequestMatcherRegistry> chainRequestMatchers(
List<RequestMatcher> requestMatchers) {
protected AuthorizedUrl chainRequestMatchers(List<RequestMatcher> requestMatchers) {
this.unmappedMatchers = requestMatchers;
return new AuthorizedUrl<>(
return new AuthorizedUrl(
(manager) -> AuthorizeHttpRequestsConfigurer.this.addMapping(requestMatchers, manager));
}

Expand Down Expand Up @@ -416,8 +415,8 @@ public H and() {
* @see AbstractRequestMatcherRegistry
* @see AuthorizeHttpRequestsConfigurer
*/
public final class AuthorizationManagerServletRequestMatcherRegistry extends
AbstractRequestMatcherBuilderRegistry<AuthorizedUrl<AuthorizationManagerServletRequestMatcherRegistry>> {
public final class AuthorizationManagerServletRequestMatcherRegistry
extends AbstractRequestMatcherBuilderRegistry<ServletAuthorizedUrl> {

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

@Override
protected AuthorizedUrl<AuthorizationManagerServletRequestMatcherRegistry> chainRequestMatchers(
List<RequestMatcher> requestMatchers) {
protected ServletAuthorizedUrl chainRequestMatchers(List<RequestMatcher> requestMatchers) {
this.unmappedMatchers = requestMatchers;
return new AuthorizedUrl<>((manager) -> addMapping(requestMatchers, manager));
return new ServletAuthorizedUrl((manager) -> addMapping(requestMatchers, manager));
}

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

}

/**
* An object that allows configuring the {@link AuthorizationManager} for
* {@link RequestMatcher}s.
*
* @author Josh Cummings
* @since 6.2
*/
public final class ServletAuthorizedUrl {

private final Function<AuthorizationManager<RequestAuthorizationContext>, AuthorizationManagerServletRequestMatcherRegistry> registrar;

ServletAuthorizedUrl(
Function<AuthorizationManager<RequestAuthorizationContext>, AuthorizationManagerServletRequestMatcherRegistry> registrar) {
this.registrar = registrar;
}

/**
* Specify that URLs are allowed by anyone.
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public AuthorizationManagerServletRequestMatcherRegistry permitAll() {
return access(permitAllAuthorizationManager);
}

/**
* Specify that URLs are not allowed by anyone.
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public AuthorizationManagerServletRequestMatcherRegistry denyAll() {
return access((a, o) -> new AuthorizationDecision(false));
}

/**
* Specifies a user requires a role.
* @param role the role that should be required which is prepended with ROLE_
* automatically (i.e. USER, ADMIN, etc). It should not start with ROLE_
* @return {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public AuthorizationManagerServletRequestMatcherRegistry hasRole(String role) {
return access(withRoleHierarchy(AuthorityAuthorizationManager
.hasAnyRole(AuthorizeHttpRequestsConfigurer.this.rolePrefix, new String[] { role })));
}

/**
* Specifies that a user requires one of many roles.
* @param roles the roles that the user should have at least one of (i.e.
* ADMIN, USER, etc). Each role should not start with ROLE_ since it is
* automatically prepended already
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public AuthorizationManagerServletRequestMatcherRegistry hasAnyRole(String... roles) {
return access(withRoleHierarchy(AuthorityAuthorizationManager
.hasAnyRole(AuthorizeHttpRequestsConfigurer.this.rolePrefix, roles)));
}

/**
* Specifies a user requires an authority.
* @param authority the authority that should be required
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public AuthorizationManagerServletRequestMatcherRegistry hasAuthority(String authority) {
return access(withRoleHierarchy(AuthorityAuthorizationManager.hasAuthority(authority)));
}

/**
* Specifies that a user requires one of many authorities.
* @param authorities the authorities that the user should have at least one
* of (i.e. ROLE_USER, ROLE_ADMIN, etc)
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public AuthorizationManagerServletRequestMatcherRegistry hasAnyAuthority(String... authorities) {
return access(withRoleHierarchy(AuthorityAuthorizationManager.hasAnyAuthority(authorities)));
}

private AuthorityAuthorizationManager<RequestAuthorizationContext> withRoleHierarchy(
AuthorityAuthorizationManager<RequestAuthorizationContext> manager) {
manager.setRoleHierarchy(AuthorizeHttpRequestsConfigurer.this.roleHierarchy.get());
return manager;
}

/**
* Specify that URLs are allowed by any authenticated user.
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public AuthorizationManagerServletRequestMatcherRegistry authenticated() {
return access(AuthenticatedAuthorizationManager.authenticated());
}

/**
* Specify that URLs are allowed by users who have authenticated and were not
* "remembered".
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customization
* @see RememberMeConfigurer
*/
public AuthorizationManagerServletRequestMatcherRegistry fullyAuthenticated() {
return access(AuthenticatedAuthorizationManager.fullyAuthenticated());
}

/**
* Specify that URLs are allowed by users that have been remembered.
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customization
* @since 5.8
* @see RememberMeConfigurer
*/
public AuthorizationManagerServletRequestMatcherRegistry rememberMe() {
return access(AuthenticatedAuthorizationManager.rememberMe());
}

/**
* Specify that URLs are allowed by anonymous users.
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customization
* @since 5.8
*/
public AuthorizationManagerServletRequestMatcherRegistry anonymous() {
return access(AuthenticatedAuthorizationManager.anonymous());
}

/**
* Allows specifying a custom {@link AuthorizationManager}.
* @param manager the {@link AuthorizationManager} to use
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public AuthorizationManagerServletRequestMatcherRegistry access(
AuthorizationManager<RequestAuthorizationContext> manager) {
Assert.notNull(manager, "manager cannot be null");
return this.registrar.apply(manager);
}

}

}

/**
Expand All @@ -462,11 +601,12 @@ private AuthorizationManagerServletRequestMatcherRegistry addMapping(List<Reques
*
* @author Evgeniy Cheban
*/
public class AuthorizedUrl<R> {
public class AuthorizedUrl {

private final Function<AuthorizationManager<RequestAuthorizationContext>, R> registrar;
private final Function<AuthorizationManager<RequestAuthorizationContext>, AuthorizationManagerRequestMatcherRegistry> registrar;

AuthorizedUrl(Function<AuthorizationManager<RequestAuthorizationContext>, R> registrar) {
AuthorizedUrl(
Function<AuthorizationManager<RequestAuthorizationContext>, AuthorizationManagerRequestMatcherRegistry> registrar) {
this.registrar = registrar;
}

Expand All @@ -475,7 +615,7 @@ public class AuthorizedUrl<R> {
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public R permitAll() {
public AuthorizationManagerRequestMatcherRegistry permitAll() {
return access(permitAllAuthorizationManager);
}

Expand All @@ -484,7 +624,7 @@ public R permitAll() {
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public R denyAll() {
public AuthorizationManagerRequestMatcherRegistry denyAll() {
return access((a, o) -> new AuthorizationDecision(false));
}

Expand All @@ -495,7 +635,7 @@ public R denyAll() {
* @return {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public R hasRole(String role) {
public AuthorizationManagerRequestMatcherRegistry hasRole(String role) {
return access(withRoleHierarchy(AuthorityAuthorizationManager
.hasAnyRole(AuthorizeHttpRequestsConfigurer.this.rolePrefix, new String[] { role })));
}
Expand All @@ -508,7 +648,7 @@ public R hasRole(String role) {
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public R hasAnyRole(String... roles) {
public AuthorizationManagerRequestMatcherRegistry hasAnyRole(String... roles) {
return access(withRoleHierarchy(
AuthorityAuthorizationManager.hasAnyRole(AuthorizeHttpRequestsConfigurer.this.rolePrefix, roles)));
}
Expand All @@ -519,7 +659,7 @@ public R hasAnyRole(String... roles) {
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public R hasAuthority(String authority) {
public AuthorizationManagerRequestMatcherRegistry hasAuthority(String authority) {
return access(withRoleHierarchy(AuthorityAuthorizationManager.hasAuthority(authority)));
}

Expand All @@ -530,7 +670,7 @@ public R hasAuthority(String authority) {
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public R hasAnyAuthority(String... authorities) {
public AuthorizationManagerRequestMatcherRegistry hasAnyAuthority(String... authorities) {
return access(withRoleHierarchy(AuthorityAuthorizationManager.hasAnyAuthority(authorities)));
}

Expand All @@ -545,7 +685,7 @@ private AuthorityAuthorizationManager<RequestAuthorizationContext> withRoleHiera
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public R authenticated() {
public AuthorizationManagerRequestMatcherRegistry authenticated() {
return access(AuthenticatedAuthorizationManager.authenticated());
}

Expand All @@ -557,7 +697,7 @@ public R authenticated() {
* @since 5.8
* @see RememberMeConfigurer
*/
public R fullyAuthenticated() {
public AuthorizationManagerRequestMatcherRegistry fullyAuthenticated() {
return access(AuthenticatedAuthorizationManager.fullyAuthenticated());
}

Expand All @@ -568,7 +708,7 @@ public R fullyAuthenticated() {
* @since 5.8
* @see RememberMeConfigurer
*/
public R rememberMe() {
public AuthorizationManagerRequestMatcherRegistry rememberMe() {
return access(AuthenticatedAuthorizationManager.rememberMe());
}

Expand All @@ -578,7 +718,7 @@ public R rememberMe() {
* customization
* @since 5.8
*/
public R anonymous() {
public AuthorizationManagerRequestMatcherRegistry anonymous() {
return access(AuthenticatedAuthorizationManager.anonymous());
}

Expand All @@ -588,7 +728,8 @@ public R anonymous() {
* @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
* customizations
*/
public R access(AuthorizationManager<RequestAuthorizationContext> manager) {
public AuthorizationManagerRequestMatcherRegistry access(
AuthorizationManager<RequestAuthorizationContext> manager) {
Assert.notNull(manager, "manager cannot be null");
return this.registrar.apply(manager);
}
Expand Down

0 comments on commit 96ebab3

Please sign in to comment.