Skip to content

Commit 1ec2047

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

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

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: 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)