Skip to content

Commit 8ee5742

Browse files
Max BatischevMax Batischev
Max Batischev
authored and
Max Batischev
committed
Add support AuthorizationResult for AuthorizationManager
Added a new authorization method to AuthorizationManager that returns AuthorizationResult. Closes gh-14843
1 parent c8e5fbf commit 8ee5742

File tree

40 files changed

+210
-171
lines changed

40 files changed

+210
-171
lines changed

config/src/main/java/org/springframework/security/config/annotation/method/configuration/DeferringObservationAuthorizationManager.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import org.aopalliance.intercept.MethodInvocation;
2323

2424
import org.springframework.beans.factory.ObjectProvider;
25-
import org.springframework.security.authorization.AuthorizationDecision;
2625
import org.springframework.security.authorization.AuthorizationManager;
2726
import org.springframework.security.authorization.AuthorizationResult;
2827
import org.springframework.security.authorization.ObservationAuthorizationManager;
@@ -61,8 +60,8 @@ final class DeferringObservationAuthorizationManager<T>
6160
}
6261

6362
@Override
64-
public AuthorizationDecision check(Supplier<Authentication> authentication, T object) {
65-
return this.delegate.get().check(authentication, object);
63+
public AuthorizationResult authorize(Supplier<Authentication> authentication, T object) {
64+
return this.delegate.get().authorize(authentication, object);
6665
}
6766

6867
@Override

config/src/main/java/org/springframework/security/config/http/DefaultFilterChainValidator.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,8 +32,8 @@
3232
import org.springframework.security.access.ConfigAttribute;
3333
import org.springframework.security.authentication.AnonymousAuthenticationToken;
3434
import org.springframework.security.authentication.TestingAuthenticationToken;
35-
import org.springframework.security.authorization.AuthorizationDecision;
3635
import org.springframework.security.authorization.AuthorizationManager;
36+
import org.springframework.security.authorization.AuthorizationResult;
3737
import org.springframework.security.core.Authentication;
3838
import org.springframework.security.web.DefaultSecurityFilterChain;
3939
import org.springframework.security.web.FilterChainProxy;
@@ -221,7 +221,8 @@ private boolean checkLoginPageIsPublic(List<Filter> filters, FilterInvocation lo
221221
AuthorizationManager<HttpServletRequest> authorizationManager = authorizationFilter
222222
.getAuthorizationManager();
223223
try {
224-
AuthorizationDecision decision = authorizationManager.check(() -> TEST, loginRequest.getHttpRequest());
224+
AuthorizationResult decision = authorizationManager.authorize(() -> TEST,
225+
loginRequest.getHttpRequest());
225226
return decision != null && decision.isGranted();
226227
}
227228
catch (Exception ex) {
@@ -252,7 +253,8 @@ private Supplier<Boolean> deriveAnonymousCheck(List<Filter> filters, FilterInvoc
252253
return () -> {
253254
AuthorizationManager<HttpServletRequest> authorizationManager = authorizationFilter
254255
.getAuthorizationManager();
255-
AuthorizationDecision decision = authorizationManager.check(() -> token, loginRequest.getHttpRequest());
256+
AuthorizationResult decision = authorizationManager.authorize(() -> token,
257+
loginRequest.getHttpRequest());
256258
return decision != null && decision.isGranted();
257259
};
258260
}

config/src/main/java/org/springframework/security/config/method/PointcutDelegatingAuthorizationManager.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.aop.support.AopUtils;
2626
import org.springframework.security.authorization.AuthorizationDecision;
2727
import org.springframework.security.authorization.AuthorizationManager;
28+
import org.springframework.security.authorization.AuthorizationResult;
2829
import org.springframework.security.core.Authentication;
2930

3031
class PointcutDelegatingAuthorizationManager implements AuthorizationManager<MethodInvocation> {
@@ -36,12 +37,12 @@ class PointcutDelegatingAuthorizationManager implements AuthorizationManager<Met
3637
}
3738

3839
@Override
39-
public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocation object) {
40+
public AuthorizationResult authorize(Supplier<Authentication> authentication, MethodInvocation object) {
4041
for (Map.Entry<Pointcut, AuthorizationManager<MethodInvocation>> entry : this.managers.entrySet()) {
4142
Class<?> targetClass = (object.getThis() != null) ? AopUtils.getTargetClass(object.getThis()) : null;
4243
if (entry.getKey().getClassFilter().matches(targetClass)
4344
&& entry.getKey().getMethodMatcher().matches(object.getMethod(), targetClass)) {
44-
return entry.getValue().check(authentication, object);
45+
return entry.getValue().authorize(authentication, object);
4546
}
4647
}
4748
return new AuthorizationDecision(false);

config/src/main/java/org/springframework/security/config/websocket/WebSocketMessageBrokerSecurityBeanDefinitionParser.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@
4949
import org.springframework.security.access.vote.ConsensusBased;
5050
import org.springframework.security.authorization.AuthorizationDecision;
5151
import org.springframework.security.authorization.AuthorizationManager;
52+
import org.springframework.security.authorization.AuthorizationResult;
5253
import org.springframework.security.config.Elements;
5354
import org.springframework.security.core.Authentication;
5455
import org.springframework.security.core.context.SecurityContextHolder;
@@ -455,7 +456,7 @@ private ExpressionBasedAuthorizationManager(
455456
}
456457

457458
@Override
458-
public AuthorizationDecision check(Supplier<Authentication> authentication,
459+
public AuthorizationResult authorize(Supplier<Authentication> authentication,
459460
MessageAuthorizationContext<?> object) {
460461
EvaluationContext context = this.expressionHandler.createEvaluationContext(authentication, object);
461462
boolean granted = ExpressionUtils.evaluateAsBoolean(this.expression, context);

config/src/test/java/org/springframework/security/config/annotation/web/configurers/AuthorizeHttpRequestsConfigurerTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -131,15 +131,15 @@ public void configureMvcMatcherAccessAuthorizationManagerWhenNotNullThenVerifyUs
131131
CustomAuthorizationManagerConfig.authorizationManager = mock(AuthorizationManager.class);
132132
this.spring.register(CustomAuthorizationManagerConfig.class, BasicController.class).autowire();
133133
this.mvc.perform(get("/")).andExpect(status().isOk());
134-
verify(CustomAuthorizationManagerConfig.authorizationManager).check(any(), any());
134+
verify(CustomAuthorizationManagerConfig.authorizationManager).authorize(any(), any());
135135
}
136136

137137
@Test
138138
public void configureNoParameterMvcMatcherAccessAuthorizationManagerWhenNotNullThenVerifyUse() throws Exception {
139139
CustomAuthorizationManagerNoParameterConfig.authorizationManager = mock(AuthorizationManager.class);
140140
this.spring.register(CustomAuthorizationManagerNoParameterConfig.class, BasicController.class).autowire();
141141
this.mvc.perform(get("/")).andExpect(status().isOk());
142-
verify(CustomAuthorizationManagerNoParameterConfig.authorizationManager).check(any(), any());
142+
verify(CustomAuthorizationManagerNoParameterConfig.authorizationManager).authorize(any(), any());
143143
}
144144

145145
@Test

config/src/test/java/org/springframework/security/config/http/DefaultFilterChainValidatorTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,7 +112,7 @@ public void validateCheckLoginPageIsntProtectedThrowsIllegalArgumentException()
112112

113113
@Test
114114
public void validateCheckLoginPageAllowsAnonymous() {
115-
given(this.authorizationManager.check(any(), any())).willReturn(new AuthorizationDecision(false));
115+
given(this.authorizationManager.authorize(any(), any())).willReturn(new AuthorizationDecision(false));
116116
this.validator.validate(this.chainAuthorizationFilter);
117117
verify(this.logger).warn("Anonymous access to the login page doesn't appear to be enabled. "
118118
+ "This is almost certainly an error. Please check your configuration allows unauthenticated "

config/src/test/java/org/springframework/security/config/http/HttpConfigTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,13 +90,13 @@ public void getWhenUsingAuthorizationManagerThenRedirectsToLogin() throws Except
9090
this.spring.configLocations(this.xml("AuthorizationManager")).autowire();
9191
AuthorizationManager<HttpServletRequest> authorizationManager = this.spring.getContext()
9292
.getBean(AuthorizationManager.class);
93-
given(authorizationManager.check(any(), any())).willReturn(new AuthorizationDecision(false));
93+
given(authorizationManager.authorize(any(), any())).willReturn(new AuthorizationDecision(false));
9494
// @formatter:off
9595
this.mvc.perform(get("/"))
9696
.andExpect(status().isFound())
9797
.andExpect(redirectedUrl("http://localhost/login"));
9898
// @formatter:on
99-
verify(authorizationManager).check(any(), any());
99+
verify(authorizationManager).authorize(any(), any());
100100
}
101101

102102
@Test

config/src/test/java/org/springframework/security/config/method/InterceptMethodsBeanDefinitionDecoratorTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -168,9 +168,9 @@ public void transactionalAuthorizationManagerMethodsShouldBeSecured() {
168168

169169
@Test
170170
public void targetCustomAuthorizationManagerUsed() {
171-
given(this.mockAuthorizationManager.check(any(), any())).willReturn(new AuthorizationDecision(true));
171+
given(this.mockAuthorizationManager.authorize(any(), any())).willReturn(new AuthorizationDecision(true));
172172
this.targetCustomAuthorizationManager.doSomething();
173-
verify(this.mockAuthorizationManager).check(any(), any());
173+
verify(this.mockAuthorizationManager).authorize(any(), any());
174174
}
175175

176176
@Override

config/src/test/java/org/springframework/security/config/method/MethodSecurityBeanDefinitionParserTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
4040
import org.springframework.security.authorization.AuthorizationDecision;
4141
import org.springframework.security.authorization.AuthorizationManager;
42+
import org.springframework.security.authorization.AuthorizationResult;
4243
import org.springframework.security.config.annotation.method.configuration.MethodSecurityService;
4344
import org.springframework.security.config.test.SpringTestContext;
4445
import org.springframework.security.config.test.SpringTestContextExtension;
@@ -463,7 +464,7 @@ public boolean hasPermission(Authentication authentication, Serializable targetI
463464
static class MyAuthorizationManager implements AuthorizationManager<MethodInvocation> {
464465

465466
@Override
466-
public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocation object) {
467+
public AuthorizationResult authorize(Supplier<Authentication> authentication, MethodInvocation object) {
467468
return new AuthorizationDecision("bob".equals(authentication.get().getName()));
468469
}
469470

config/src/test/java/org/springframework/security/config/websocket/WebSocketMessageBrokerConfigTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -489,11 +489,11 @@ public void sendWhenCustomAuthorizationManagerThenAuthorizesAccordingly() {
489489
this.spring.configLocations(xml("CustomAuthorizationManagerConfig")).autowire();
490490
AuthorizationManager<Message<?>> authorizationManager = this.spring.getContext()
491491
.getBean(AuthorizationManager.class);
492-
given(authorizationManager.check(any(), any())).willReturn(new AuthorizationDecision(false));
492+
given(authorizationManager.authorize(any(), any())).willReturn(new AuthorizationDecision(false));
493493
Message<?> message = message("/any");
494494
assertThatExceptionOfType(Exception.class).isThrownBy(send(message))
495495
.withCauseInstanceOf(AccessDeniedException.class);
496-
verify(authorizationManager).check(any(), any());
496+
verify(authorizationManager).authorize(any(), any());
497497
}
498498

499499
private String xml(String configName) {

core/src/main/java/org/springframework/security/authorization/AuthenticatedAuthorizationManager.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -106,12 +106,13 @@ public static <T> AuthenticatedAuthorizationManager<T> anonymous() {
106106

107107
/**
108108
* Determines if the current user is authorized according to the given strategy.
109-
* @param authentication the {@link Supplier} of the {@link Authentication} to check
110-
* @param object the {@link T} object to check
109+
* @param authentication the {@link Supplier} of the {@link Authentication} to
110+
* authorize
111+
* @param object the {@link T} object to authorize
111112
* @return an {@link AuthorizationDecision}
112113
*/
113114
@Override
114-
public AuthorizationDecision check(Supplier<Authentication> authentication, T object) {
115+
public AuthorizationResult authorize(Supplier<Authentication> authentication, T object) {
115116
boolean granted = this.authorizationStrategy.isGranted(authentication.get());
116117
return new AuthorizationDecision(granted);
117118
}

core/src/main/java/org/springframework/security/authorization/AuthoritiesAuthorizationManager.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,13 +50,13 @@ public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
5050
/**
5151
* Determines if the current user is authorized by evaluating if the
5252
* {@link Authentication} contains any of specified authorities.
53-
* @param authentication the {@link Supplier} of the {@link Authentication} to check
54-
* @param authorities the collection of authority strings to check
53+
* @param authentication the {@link Supplier} of the {@link Authentication} to
54+
* authorize
55+
* @param authorities the collection of authority strings to authorize
5556
* @return an {@link AuthorityAuthorizationDecision}
5657
*/
5758
@Override
58-
public AuthorityAuthorizationDecision check(Supplier<Authentication> authentication,
59-
Collection<String> authorities) {
59+
public AuthorizationResult authorize(Supplier<Authentication> authentication, Collection<String> authorities) {
6060
boolean granted = isGranted(authentication.get(), authorities);
6161
return new AuthorityAuthorizationDecision(granted, AuthorityUtils.createAuthorityList(authorities));
6262
}

core/src/main/java/org/springframework/security/authorization/AuthorityAuthorizationManager.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@
2626

2727
/**
2828
* An {@link AuthorizationManager} that determines if the current user is authorized by
29-
* evaluating if the {@link Authentication} contains a specified authority.
29+
* evaluating if the {@link AuthorizationResult} contains a specified authority.
3030
*
3131
* @param <T> the type of object being authorized.
3232
* @author Evgeniy Cheban
@@ -136,13 +136,14 @@ private static String[] toNamedRolesArray(String rolePrefix, String[] roles) {
136136
/**
137137
* Determines if the current user is authorized by evaluating if the
138138
* {@link Authentication} contains a specified authority.
139-
* @param authentication the {@link Supplier} of the {@link Authentication} to check
140-
* @param object the {@link T} object to check
141-
* @return an {@link AuthorizationDecision}
139+
* @param authentication the {@link Supplier} of the {@link Authentication} to
140+
* authorize
141+
* @param object the {@link T} object to authorize
142+
* @return an {@link AuthorizationResult}
142143
*/
143144
@Override
144-
public AuthorizationDecision check(Supplier<Authentication> authentication, T object) {
145-
return this.delegate.check(authentication, this.authorities);
145+
public AuthorizationResult authorize(Supplier<Authentication> authentication, T object) {
146+
return this.delegate.authorize(authentication, this.authorities);
146147
}
147148

148149
@Override

core/src/main/java/org/springframework/security/authorization/AuthorizationManager.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ public interface AuthorizationManager<T> {
3939
* @throws AccessDeniedException if access is not granted
4040
*/
4141
default void verify(Supplier<Authentication> authentication, T object) {
42-
AuthorizationDecision decision = check(authentication, object);
42+
AuthorizationResult decision = authorize(authentication, object);
4343
if (decision != null && !decision.isGranted()) {
4444
throw new AccessDeniedException("Access Denied");
4545
}
@@ -50,8 +50,23 @@ default void verify(Supplier<Authentication> authentication, T object) {
5050
* @param authentication the {@link Supplier} of the {@link Authentication} to check
5151
* @param object the {@link T} object to check
5252
* @return an {@link AuthorizationDecision} or null if no decision could be made
53+
* @deprecated Use {{@link #authorize(Supplier, Object)}} instead
5354
*/
55+
@Deprecated(forRemoval = true)
5456
@Nullable
55-
AuthorizationDecision check(Supplier<Authentication> authentication, T object);
57+
default AuthorizationDecision check(Supplier<Authentication> authentication, T object) {
58+
return (AuthorizationDecision) authorize(authentication, object);
59+
}
60+
61+
/**
62+
* Determines if access should be granted for a specific authentication and object.
63+
* @param authentication the {@link Supplier} of the {@link Authentication} to
64+
* authorize
65+
* @param object the {@link T} object to authorize
66+
* @return an {@link AuthorizationResult} or null if no result could be made
67+
* @since 6.3
68+
*/
69+
@Nullable
70+
AuthorizationResult authorize(Supplier<Authentication> authentication, T object);
5671

5772
}

0 commit comments

Comments
 (0)