|
@@ -17,6 +17,7 @@
|
|
|
package org.springframework.security.config.annotation.web.configurers;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
import io.micrometer.observation.ObservationRegistry;
|
|
@@ -37,6 +38,7 @@ import org.springframework.security.config.annotation.ObjectPostProcessor;
|
|
|
import org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry;
|
|
|
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
|
|
|
import org.springframework.security.config.core.GrantedAuthorityDefaults;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.web.access.intercept.AuthorizationFilter;
|
|
|
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
|
|
|
import org.springframework.security.web.access.intercept.RequestMatcherDelegatingAuthorizationManager;
|
|
@@ -387,6 +389,21 @@ public final class AuthorizeHttpRequestsConfigurer<H extends HttpSecurityBuilder
|
|
|
return access(AuthenticatedAuthorizationManager.anonymous());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Specify that a path variable in URL to be compared.
|
|
|
+ *
|
|
|
+ * <p>
|
|
|
+ * For example, <pre>
|
|
|
+ * requestMatchers("/user/{username}").hasVariable("username").equalTo(Authentication::getName)
|
|
|
+ * </pre>
|
|
|
+ * @param variable the variable in URL template to compare.
|
|
|
+ * @return {@link AuthorizedUrlVariable} for further customization.
|
|
|
+ * @since 6.3
|
|
|
+ */
|
|
|
+ public AuthorizedUrlVariable hasVariable(String variable) {
|
|
|
+ return new AuthorizedUrlVariable(variable);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Allows specifying a custom {@link AuthorizationManager}.
|
|
|
* @param manager the {@link AuthorizationManager} to use
|
|
@@ -401,6 +418,41 @@ public final class AuthorizeHttpRequestsConfigurer<H extends HttpSecurityBuilder
|
|
|
: AuthorizeHttpRequestsConfigurer.this.addMapping(this.matchers, manager);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * An object that allows configuring {@link RequestMatcher}s with URI path
|
|
|
+ * variables
|
|
|
+ *
|
|
|
+ * @author Taehong Kim
|
|
|
+ * @since 6.3
|
|
|
+ */
|
|
|
+ public final class AuthorizedUrlVariable {
|
|
|
+
|
|
|
+ private final String variable;
|
|
|
+
|
|
|
+ private AuthorizedUrlVariable(String variable) {
|
|
|
+ this.variable = variable;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Compares the value of a path variable in the URI with an `Authentication`
|
|
|
+ * attribute
|
|
|
+ * <p>
|
|
|
+ * For example, <pre>
|
|
|
+ * requestMatchers("/user/{username}").hasVariable("username").equalTo(Authentication::getName));
|
|
|
+ * </pre>
|
|
|
+ * @param function a function to get value from {@link Authentication}.
|
|
|
+ * @return the {@link AuthorizationManagerRequestMatcherRegistry} for further
|
|
|
+ * customization.
|
|
|
+ */
|
|
|
+ public AuthorizationManagerRequestMatcherRegistry equalTo(Function<Authentication, String> function) {
|
|
|
+ return access((auth, requestContext) -> {
|
|
|
+ String value = requestContext.getVariables().get(this.variable);
|
|
|
+ return new AuthorizationDecision(function.apply(auth.get()).equals(value));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|