|
@@ -17,6 +17,7 @@
|
|
|
package org.springframework.security.messaging.access.intercept;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Supplier;
|
|
@@ -34,10 +35,13 @@ import org.springframework.security.authorization.AuthorizationManager;
|
|
|
import org.springframework.security.authorization.SingleResultAuthorizationManager;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.messaging.util.matcher.MessageMatcher;
|
|
|
+import org.springframework.security.messaging.util.matcher.MessageMatcherFactory;
|
|
|
+import org.springframework.security.messaging.util.matcher.PathPatternMessageMatcher;
|
|
|
import org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher;
|
|
|
import org.springframework.security.messaging.util.matcher.SimpMessageTypeMatcher;
|
|
|
import org.springframework.util.AntPathMatcher;
|
|
|
import org.springframework.util.Assert;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.PathMatcher;
|
|
|
import org.springframework.util.function.SingletonSupplier;
|
|
|
|
|
@@ -85,15 +89,17 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
}
|
|
|
|
|
|
private MessageAuthorizationContext<?> authorizationContext(MessageMatcher<?> matcher, Message<?> message) {
|
|
|
- if (!matcher.matches((Message) message)) {
|
|
|
+ MessageMatcher.MatchResult matchResult = matcher.matcher((Message) message);
|
|
|
+ if (!matchResult.isMatch()) {
|
|
|
return null;
|
|
|
}
|
|
|
- if (matcher instanceof SimpDestinationMessageMatcher simp) {
|
|
|
- return new MessageAuthorizationContext<>(message, simp.extractPathVariables(message));
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(matchResult.getVariables())) {
|
|
|
+ return new MessageAuthorizationContext<>(message, matchResult.getVariables());
|
|
|
}
|
|
|
- if (matcher instanceof Builder.LazySimpDestinationMessageMatcher) {
|
|
|
- Builder.LazySimpDestinationMessageMatcher path = (Builder.LazySimpDestinationMessageMatcher) matcher;
|
|
|
- return new MessageAuthorizationContext<>(message, path.extractPathVariables(message));
|
|
|
+
|
|
|
+ if (matcher instanceof Builder.LazySimpDestinationMessageMatcher pathMatcher) {
|
|
|
+ return new MessageAuthorizationContext<>(message, pathMatcher.extractPathVariables(message));
|
|
|
}
|
|
|
return new MessageAuthorizationContext<>(message);
|
|
|
}
|
|
@@ -113,6 +119,7 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
|
|
|
private final List<Entry<AuthorizationManager<MessageAuthorizationContext<?>>>> mappings = new ArrayList<>();
|
|
|
|
|
|
+ @Deprecated
|
|
|
private Supplier<PathMatcher> pathMatcher = AntPathMatcher::new;
|
|
|
|
|
|
public Builder() {
|
|
@@ -133,11 +140,11 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
* @return the Expression to associate
|
|
|
*/
|
|
|
public Builder.Constraint nullDestMatcher() {
|
|
|
- return matchers(SimpDestinationMessageMatcher.NULL_DESTINATION_MATCHER);
|
|
|
+ return matchers(PathPatternMessageMatcher.NULL_DESTINATION_MATCHER);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances.
|
|
|
+ * Maps a {@link List} of {@link SimpMessageTypeMatcher} instances.
|
|
|
* @param typesToMatch the {@link SimpMessageType} instance to match on
|
|
|
* @return the {@link Builder.Constraint} associated to the matchers.
|
|
|
*/
|
|
@@ -151,56 +158,58 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances without
|
|
|
- * regard to the {@link SimpMessageType}. If no destination is found on the
|
|
|
- * Message, then the Matcher returns false.
|
|
|
- * @param patterns the patterns to create
|
|
|
- * {@link org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher}
|
|
|
- * from.
|
|
|
+ * Maps a {@link List} of {@link SimpDestinationMessageMatcher} (or
|
|
|
+ * {@link PathPatternMessageMatcher} if the application has configured a
|
|
|
+ * {@link org.springframework.security.messaging.util.matcher.PathPatternMessageMatcherBuilderFactoryBean})
|
|
|
+ * instances without regard to the {@link SimpMessageType}. If no destination is
|
|
|
+ * found on the Message, then the Matcher returns false.
|
|
|
+ * @param patterns the patterns to create {@code MessageMatcher}s from.
|
|
|
*/
|
|
|
public Builder.Constraint simpDestMatchers(String... patterns) {
|
|
|
return simpDestMatchers(null, patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances that
|
|
|
- * match on {@code SimpMessageType.MESSAGE}. If no destination is found on the
|
|
|
- * Message, then the Matcher returns false.
|
|
|
- * @param patterns the patterns to create
|
|
|
- * {@link org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher}
|
|
|
- * from.
|
|
|
+ * Maps a {@link List} of {@link SimpDestinationMessageMatcher} (or
|
|
|
+ * {@link PathPatternMessageMatcher} if the application has configured a
|
|
|
+ * {@link org.springframework.security.messaging.util.matcher.PathPatternMessageMatcherBuilderFactoryBean})
|
|
|
+ * instances that match on {@code SimpMessageType.MESSAGE}. If no destination is
|
|
|
+ * found on the Message, then the Matcher returns false.
|
|
|
+ * @param patterns the patterns to create {@code MessageMatcher}s from.
|
|
|
*/
|
|
|
public Builder.Constraint simpMessageDestMatchers(String... patterns) {
|
|
|
return simpDestMatchers(SimpMessageType.MESSAGE, patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances that
|
|
|
- * match on {@code SimpMessageType.SUBSCRIBE}. If no destination is found on the
|
|
|
- * Message, then the Matcher returns false.
|
|
|
- * @param patterns the patterns to create
|
|
|
- * {@link org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher}
|
|
|
- * from.
|
|
|
+ * Maps a {@link List} of {@link SimpDestinationMessageMatcher} (or
|
|
|
+ * {@link PathPatternMessageMatcher} if the application has configured a
|
|
|
+ * {@link org.springframework.security.messaging.util.matcher.PathPatternMessageMatcherBuilderFactoryBean})
|
|
|
+ * instances that match on {@code SimpMessageType.SUBSCRIBE}. If no destination is
|
|
|
+ * found on the Message, then the Matcher returns false.
|
|
|
+ * @param patterns the patterns to create {@code MessageMatcher}s from.
|
|
|
*/
|
|
|
public Builder.Constraint simpSubscribeDestMatchers(String... patterns) {
|
|
|
return simpDestMatchers(SimpMessageType.SUBSCRIBE, patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances. If no
|
|
|
- * destination is found on the Message, then the Matcher returns false.
|
|
|
+ * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances, or
|
|
|
+ * {@link PathPatternMessageMatcher} if the application has configured a
|
|
|
+ * {@link org.springframework.security.messaging.util.matcher.PathPatternMessageMatcherBuilderFactoryBean}.
|
|
|
+ * If no destination is found on the Message, then the Matcher returns false.
|
|
|
* @param type the {@link SimpMessageType} to match on. If null, the
|
|
|
* {@link SimpMessageType} is not considered for matching.
|
|
|
- * @param patterns the patterns to create
|
|
|
- * {@link org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher}
|
|
|
- * from.
|
|
|
+ * @param patterns the patterns to create {@code MessageMatcher}s from.
|
|
|
* @return the {@link Builder.Constraint} that is associated to the
|
|
|
* {@link MessageMatcher}
|
|
|
*/
|
|
|
private Builder.Constraint simpDestMatchers(SimpMessageType type, String... patterns) {
|
|
|
List<MessageMatcher<?>> matchers = new ArrayList<>(patterns.length);
|
|
|
for (String pattern : patterns) {
|
|
|
- MessageMatcher<Object> matcher = new LazySimpDestinationMessageMatcher(pattern, type);
|
|
|
+ MessageMatcher<Object> matcher = MessageMatcherFactory.usesPathPatterns()
|
|
|
+ ? MessageMatcherFactory.matcher(pattern, type)
|
|
|
+ : new LazySimpDestinationMessageMatcher(pattern, type);
|
|
|
matchers.add(matcher);
|
|
|
}
|
|
|
return new Builder.Constraint(matchers);
|
|
@@ -212,7 +221,9 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
* constructor of {@link AntPathMatcher}.
|
|
|
* @param pathMatcher the {@link PathMatcher} to use. Cannot be null.
|
|
|
* @return the {@link Builder} for further customization.
|
|
|
+ * @deprecated
|
|
|
*/
|
|
|
+ @Deprecated
|
|
|
public Builder simpDestPathMatcher(PathMatcher pathMatcher) {
|
|
|
Assert.notNull(pathMatcher, "pathMatcher cannot be null");
|
|
|
this.pathMatcher = () -> pathMatcher;
|
|
@@ -225,7 +236,9 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
* computation or lookup of the {@link PathMatcher}.
|
|
|
* @param pathMatcher the {@link PathMatcher} to use. Cannot be null.
|
|
|
* @return the {@link Builder} for further customization.
|
|
|
+ * @deprecated
|
|
|
*/
|
|
|
+ @Deprecated
|
|
|
public Builder simpDestPathMatcher(Supplier<PathMatcher> pathMatcher) {
|
|
|
Assert.notNull(pathMatcher, "pathMatcher cannot be null");
|
|
|
this.pathMatcher = pathMatcher;
|
|
@@ -241,9 +254,7 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
*/
|
|
|
public Builder.Constraint matchers(MessageMatcher<?>... matchers) {
|
|
|
List<MessageMatcher<?>> builders = new ArrayList<>(matchers.length);
|
|
|
- for (MessageMatcher<?> matcher : matchers) {
|
|
|
- builders.add(matcher);
|
|
|
- }
|
|
|
+ builders.addAll(Arrays.asList(matchers));
|
|
|
return new Builder.Constraint(builders);
|
|
|
}
|
|
|
|
|
@@ -382,6 +393,7 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @Deprecated
|
|
|
private final class LazySimpDestinationMessageMatcher implements MessageMatcher<Object> {
|
|
|
|
|
|
private final Supplier<SimpDestinationMessageMatcher> delegate;
|
|
@@ -421,7 +433,7 @@ public final class MessageMatcherDelegatingAuthorizationManager implements Autho
|
|
|
|
|
|
private final T entry;
|
|
|
|
|
|
- Entry(MessageMatcher requestMatcher, T entry) {
|
|
|
+ Entry(MessageMatcher<?> requestMatcher, T entry) {
|
|
|
this.messageMatcher = requestMatcher;
|
|
|
this.entry = entry;
|
|
|
}
|