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