Skip to content

Commit fbc98a2

Browse files
committed
Replace deprecated #check calls with #authorize
Closes spring-projectsgh-16936 Signed-off-by: Evgeniy Cheban <[email protected]>
1 parent 39b195c commit fbc98a2

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

Diff for: 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-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -155,15 +155,15 @@ public void configureMvcMatcherAccessAuthorizationManagerWhenNotNullThenVerifyUs
155155
CustomAuthorizationManagerConfig.authorizationManager = mock(AuthorizationManager.class);
156156
this.spring.register(CustomAuthorizationManagerConfig.class, BasicController.class).autowire();
157157
this.mvc.perform(get("/")).andExpect(status().isOk());
158-
verify(CustomAuthorizationManagerConfig.authorizationManager).check(any(), any());
158+
verify(CustomAuthorizationManagerConfig.authorizationManager).authorize(any(), any());
159159
}
160160

161161
@Test
162162
public void configureNoParameterMvcMatcherAccessAuthorizationManagerWhenNotNullThenVerifyUse() throws Exception {
163163
CustomAuthorizationManagerNoParameterConfig.authorizationManager = mock(AuthorizationManager.class);
164164
this.spring.register(CustomAuthorizationManagerNoParameterConfig.class, BasicController.class).autowire();
165165
this.mvc.perform(get("/")).andExpect(status().isOk());
166-
verify(CustomAuthorizationManagerNoParameterConfig.authorizationManager).check(any(), any());
166+
verify(CustomAuthorizationManagerNoParameterConfig.authorizationManager).authorize(any(), any());
167167
}
168168

169169
@Test

Diff for: core/src/main/java/org/springframework/security/authorization/ObservationAuthorizationManager.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -67,20 +67,33 @@ public ObservationAuthorizationManager(ObservationRegistry registry, Authorizati
6767
@Deprecated
6868
@Override
6969
public AuthorizationDecision check(Supplier<Authentication> authentication, T object) {
70+
AuthorizationResult result = authorize(authentication, object);
71+
if (result == null) {
72+
return null;
73+
}
74+
if (result instanceof AuthorizationDecision decision) {
75+
return decision;
76+
}
77+
throw new IllegalArgumentException(
78+
"Please call #authorize or ensure that the returned result is of type AuthorizationDecision");
79+
}
80+
81+
@Override
82+
public AuthorizationResult authorize(Supplier<Authentication> authentication, T object) {
7083
AuthorizationObservationContext<T> context = new AuthorizationObservationContext<>(object);
7184
Supplier<Authentication> wrapped = () -> {
7285
context.setAuthentication(authentication.get());
7386
return context.getAuthentication();
7487
};
7588
Observation observation = Observation.createNotStarted(this.convention, () -> context, this.registry).start();
7689
try (Observation.Scope scope = observation.openScope()) {
77-
AuthorizationDecision decision = this.delegate.check(wrapped, object);
78-
context.setAuthorizationResult(decision);
79-
if (decision != null && !decision.isGranted()) {
90+
AuthorizationResult result = this.delegate.authorize(wrapped, object);
91+
context.setAuthorizationResult(result);
92+
if (result != null && !result.isGranted()) {
8093
observation.error(new AccessDeniedException(
8194
this.messages.getMessage("AbstractAccessDecisionManager.accessDenied", "Access Denied")));
8295
}
83-
return decision;
96+
return result;
8497
}
8598
catch (Throwable ex) {
8699
observation.error(ex);

Diff for: core/src/test/java/org/springframework/security/authorization/ObservationAuthorizationManagerTests.java

+4-4
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-2025 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.
@@ -73,7 +73,7 @@ void setup() {
7373
@Test
7474
void verifyWhenDefaultsThenObserves() {
7575
given(this.handler.supportsContext(any())).willReturn(true);
76-
given(this.authorizationManager.check(any(), any())).willReturn(this.grant);
76+
given(this.authorizationManager.authorize(any(), any())).willReturn(this.grant);
7777
this.tested.verify(this.token, this.object);
7878
ArgumentCaptor<Observation.Context> captor = ArgumentCaptor.forClass(Observation.Context.class);
7979
verify(this.handler).onStart(captor.capture());
@@ -91,7 +91,7 @@ void verifyWhenErrorsThenObserves() {
9191
MessageSource source = mock(MessageSource.class);
9292
this.tested.setMessageSource(source);
9393
given(this.handler.supportsContext(any())).willReturn(true);
94-
given(this.authorizationManager.check(any(), any())).willReturn(this.deny);
94+
given(this.authorizationManager.authorize(any(), any())).willReturn(this.deny);
9595
given(source.getMessage(eq("AbstractAccessDecisionManager.accessDenied"), any(), any(), any()))
9696
.willReturn("accessDenied");
9797
assertThatExceptionOfType(AccessDeniedException.class)
@@ -112,7 +112,7 @@ void verifyWhenErrorsThenObserves() {
112112
@Test
113113
void verifyWhenLooksUpAuthenticationThenObserves() {
114114
given(this.handler.supportsContext(any())).willReturn(true);
115-
given(this.authorizationManager.check(any(), any())).willAnswer((invocation) -> {
115+
given(this.authorizationManager.authorize(any(), any())).willAnswer((invocation) -> {
116116
((Supplier<Authentication>) invocation.getArgument(0)).get();
117117
return this.grant;
118118
});

Diff for: messaging/src/main/java/org/springframework/security/messaging/access/intercept/MessageMatcherDelegatingAuthorizationManager.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.security.authorization.AuthorityAuthorizationManager;
3131
import org.springframework.security.authorization.AuthorizationDecision;
3232
import org.springframework.security.authorization.AuthorizationManager;
33+
import org.springframework.security.authorization.AuthorizationResult;
3334
import org.springframework.security.authorization.SingleResultAuthorizationManager;
3435
import org.springframework.security.core.Authentication;
3536
import org.springframework.security.messaging.util.matcher.MessageMatcher;
@@ -63,11 +64,24 @@ private MessageMatcherDelegatingAuthorizationManager(
6364
* @return an {@link AuthorizationDecision}. If there is no {@link MessageMatcher}
6465
* matching the message, or the {@link AuthorizationManager} could not decide, then
6566
* null is returned
66-
* @deprecated please use {@link #authorize(Supplier, Object)} instead
67+
* @deprecated please use {@link #authorize(Supplier, Message)} instead
6768
*/
6869
@Deprecated
6970
@Override
7071
public AuthorizationDecision check(Supplier<Authentication> authentication, Message<?> message) {
72+
AuthorizationResult result = authorize(authentication, message);
73+
if (result == null) {
74+
return null;
75+
}
76+
if (result instanceof AuthorizationDecision decision) {
77+
return decision;
78+
}
79+
throw new IllegalArgumentException(
80+
"Please call #authorize or ensure that the returned result is of type AuthorizationDecision");
81+
}
82+
83+
@Override
84+
public AuthorizationResult authorize(Supplier<Authentication> authentication, Message<?> message) {
7185
if (this.logger.isTraceEnabled()) {
7286
this.logger.trace(LogMessage.format("Authorizing message"));
7387
}
@@ -79,7 +93,7 @@ public AuthorizationDecision check(Supplier<Authentication> authentication, Mess
7993
if (this.logger.isTraceEnabled()) {
8094
this.logger.trace(LogMessage.format("Checking authorization on message using %s", manager));
8195
}
82-
return manager.check(authentication, authorizationContext);
96+
return manager.authorize(authentication, authorizationContext);
8397
}
8498
}
8599
this.logger.trace("Abstaining since did not find matching MessageMatcher");

Diff for: web/src/main/java/org/springframework/security/web/access/intercept/RequestMatcherDelegatingAuthorizationManager.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.security.authorization.AuthorityAuthorizationManager;
3131
import org.springframework.security.authorization.AuthorizationDecision;
3232
import org.springframework.security.authorization.AuthorizationManager;
33+
import org.springframework.security.authorization.AuthorizationResult;
3334
import org.springframework.security.authorization.SingleResultAuthorizationManager;
3435
import org.springframework.security.core.Authentication;
3536
import org.springframework.security.web.util.UrlUtils;
@@ -69,11 +70,24 @@ private RequestMatcherDelegatingAuthorizationManager(
6970
* @return an {@link AuthorizationDecision}. If there is no {@link RequestMatcher}
7071
* matching the request, or the {@link AuthorizationManager} could not decide, then
7172
* null is returned
72-
* @deprecated please use {@link #authorize(Supplier, Object)} instead
73+
* @deprecated please use {@link #authorize(Supplier, HttpServletRequest)} instead
7374
*/
7475
@Deprecated
7576
@Override
7677
public AuthorizationDecision check(Supplier<Authentication> authentication, HttpServletRequest request) {
78+
AuthorizationResult result = authorize(authentication, request);
79+
if (result == null) {
80+
return null;
81+
}
82+
if (result instanceof AuthorizationDecision decision) {
83+
return decision;
84+
}
85+
throw new IllegalArgumentException(
86+
"Please call #authorize or ensure that the returned result is of type AuthorizationDecision");
87+
}
88+
89+
@Override
90+
public AuthorizationResult authorize(Supplier<Authentication> authentication, HttpServletRequest request) {
7791
if (this.logger.isTraceEnabled()) {
7892
this.logger.trace(LogMessage.format("Authorizing %s", requestLine(request)));
7993
}
@@ -87,7 +101,7 @@ public AuthorizationDecision check(Supplier<Authentication> authentication, Http
87101
this.logger.trace(
88102
LogMessage.format("Checking authorization on %s using %s", requestLine(request), manager));
89103
}
90-
return manager.check(authentication,
104+
return manager.authorize(authentication,
91105
new RequestAuthorizationContext(request, matchResult.getVariables()));
92106
}
93107
}

0 commit comments

Comments
 (0)