From 8224b16caf3b6ac07d642ea8dfd00e85aec9203f Mon Sep 17 00:00:00 2001
From: Robert Winch <362503+rwinch@users.noreply.github.com>
Date: Wed, 11 Mar 2026 11:33:59 -0500
Subject: [PATCH] Add ConditionalAuthorizationManager
Closes gh-18919
---
.../ConditionalAuthorizationManager.java | 154 ++++++++++++++++++
.../ConditionalAuthorizationManagerTests.java | 131 +++++++++++++++
.../servlet/authorization/architecture.adoc | 10 ++
docs/modules/ROOT/pages/whats-new.adoc | 1 +
...onditionalAuthorizationManagerExample.java | 33 ++++
.../ConditionalAuthorizationManagerExample.kt | 26 +++
6 files changed, 355 insertions(+)
create mode 100644 core/src/main/java/org/springframework/security/authorization/ConditionalAuthorizationManager.java
create mode 100644 core/src/test/java/org/springframework/security/authorization/ConditionalAuthorizationManagerTests.java
create mode 100644 docs/src/test/java/org/springframework/security/docs/servlet/authorization/authzconditionalauthorizationmanager/ConditionalAuthorizationManagerExample.java
create mode 100644 docs/src/test/kotlin/org/springframework/security/kt/docs/servlet/authorization/authzconditionalauthorizationmanager/ConditionalAuthorizationManagerExample.kt
diff --git a/core/src/main/java/org/springframework/security/authorization/ConditionalAuthorizationManager.java b/core/src/main/java/org/springframework/security/authorization/ConditionalAuthorizationManager.java
new file mode 100644
index 0000000000..7daa470602
--- /dev/null
+++ b/core/src/main/java/org/springframework/security/authorization/ConditionalAuthorizationManager.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2004-present the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.authorization;
+
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+
+import org.jspecify.annotations.Nullable;
+
+import org.springframework.security.core.Authentication;
+import org.springframework.util.Assert;
+
+/**
+ * An {@link AuthorizationManager} that delegates to one of two
+ * {@link AuthorizationManager} instances based on a condition evaluated against the
+ * current {@link Authentication}.
+ *
+ * When {@link #authorize(Supplier, Object)} is invoked, the condition is evaluated. If
+ * the {@link Authentication} is non-null and the condition returns {@code true}, the
+ * {@code whenTrue} manager is used; otherwise the {@code whenFalse} manager is used.
+ *
+ * This is useful for scenarios such as requiring multi-factor authentication only when
+ * the user has registered a second factor, or applying different rules based on
+ * authentication state.
+ *
+ * @param the type of object that the authorization check is being performed on
+ * @author Rob Winch
+ * @since 7.1
+ */
+public final class ConditionalAuthorizationManager implements AuthorizationManager {
+
+ private final Predicate condition;
+
+ private final AuthorizationManager whenTrue;
+
+ private final AuthorizationManager whenFalse;
+
+ /**
+ * Creates a {@link ConditionalAuthorizationManager} that delegates to
+ * {@code whenTrue} when the condition holds for the current {@link Authentication},
+ * and to {@code whenFalse} otherwise.
+ * @param condition the condition to evaluate against the {@link Authentication} (must
+ * not be null)
+ * @param whenTrue the manager to use when the condition is true (must not be null)
+ * @param whenFalse the manager to use when the condition is false (must not be null)
+ */
+ private ConditionalAuthorizationManager(Predicate condition, AuthorizationManager whenTrue,
+ AuthorizationManager whenFalse) {
+ Assert.notNull(condition, "condition cannot be null");
+ Assert.notNull(whenTrue, "whenTrue cannot be null");
+ Assert.notNull(whenFalse, "whenFalse cannot be null");
+ this.condition = condition;
+ this.whenTrue = whenTrue;
+ this.whenFalse = whenFalse;
+ }
+
+ /**
+ * Creates a builder for a {@link ConditionalAuthorizationManager} with the given
+ * condition.
+ * @param the type of object that the authorization check is being performed on
+ * @param condition the condition to evaluate against the {@link Authentication} (must
+ * not be null)
+ * @return the builder
+ */
+ public static Builder when(Predicate condition) {
+ Assert.notNull(condition, "condition cannot be null");
+ return new Builder<>(condition);
+ }
+
+ @Override
+ public @Nullable AuthorizationResult authorize(Supplier extends @Nullable Authentication> authentication,
+ T object) {
+ Authentication auth = authentication.get();
+ if (auth != null && this.condition.test(auth)) {
+ return this.whenTrue.authorize(authentication, object);
+ }
+ return this.whenFalse.authorize(authentication, object);
+ }
+
+ /**
+ * A builder for {@link ConditionalAuthorizationManager}.
+ *
+ * @param the type of object that the authorization check is being performed on
+ * @author Rob Winch
+ * @since 7.1
+ */
+ public static final class Builder {
+
+ private final Predicate condition;
+
+ private @Nullable AuthorizationManager whenTrue;
+
+ private @Nullable AuthorizationManager whenFalse;
+
+ private Builder(Predicate condition) {
+ this.condition = condition;
+ }
+
+ /**
+ * Sets the {@link AuthorizationManager} to use when the condition is true.
+ * @param whenTrue the manager to use when the condition is true (must not be
+ * null)
+ * @return the builder
+ */
+ public Builder whenTrue(AuthorizationManager whenTrue) {
+ Assert.notNull(whenTrue, "whenTrue cannot be null");
+ this.whenTrue = whenTrue;
+ return this;
+ }
+
+ /**
+ * Sets the {@link AuthorizationManager} to use when the condition is false.
+ * Defaults to {@link SingleResultAuthorizationManager#permitAll()} if not set.
+ * @param whenFalse the manager to use when the condition is false (must not be
+ * null)
+ * @return the builder
+ */
+ public Builder whenFalse(AuthorizationManager whenFalse) {
+ Assert.notNull(whenFalse, "whenFalse cannot be null");
+ this.whenFalse = whenFalse;
+ return this;
+ }
+
+ /**
+ * Builds the {@link ConditionalAuthorizationManager}.
+ * @return the {@link ConditionalAuthorizationManager}
+ */
+ @SuppressWarnings("unchecked")
+ public ConditionalAuthorizationManager build() {
+ Assert.state(this.whenTrue != null, "whenTrue is required");
+ AuthorizationManager whenFalse = this.whenFalse;
+ if (whenFalse == null) {
+ whenFalse = (AuthorizationManager) SingleResultAuthorizationManager.permitAll();
+ }
+ return new ConditionalAuthorizationManager<>(this.condition, this.whenTrue, whenFalse);
+ }
+
+ }
+
+}
diff --git a/core/src/test/java/org/springframework/security/authorization/ConditionalAuthorizationManagerTests.java b/core/src/test/java/org/springframework/security/authorization/ConditionalAuthorizationManagerTests.java
new file mode 100644
index 0000000000..86f03c254c
--- /dev/null
+++ b/core/src/test/java/org/springframework/security/authorization/ConditionalAuthorizationManagerTests.java
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2004-present the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.authorization;
+
+import org.junit.jupiter.api.Test;
+
+import org.springframework.security.authentication.TestingAuthenticationToken;
+import org.springframework.security.core.Authentication;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
+
+/**
+ * Tests for {@link ConditionalAuthorizationManager}.
+ *
+ * @author Rob Winch
+ */
+public class ConditionalAuthorizationManagerTests {
+
+ @Test
+ void authorizeWhenAuthenticationIsNullThenUsesWhenFalse() {
+ ConditionalAuthorizationManager