3 changed files with 0 additions and 369 deletions
@ -1,52 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2002-2025 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.messaging.util.matcher; |
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext; |
|
||||||
import org.springframework.messaging.simp.SimpMessageType; |
|
||||||
|
|
||||||
/** |
|
||||||
* This utility exists only to facilitate applications opting into using path patterns in |
|
||||||
* the Message Security DSL. It is for internal use only. |
|
||||||
* |
|
||||||
* @deprecated |
|
||||||
*/ |
|
||||||
@Deprecated(forRemoval = true) |
|
||||||
public final class MessageMatcherFactory { |
|
||||||
|
|
||||||
private static PathPatternMessageMatcher.Builder builder; |
|
||||||
|
|
||||||
public static void setApplicationContext(ApplicationContext context) { |
|
||||||
builder = context.getBeanProvider(PathPatternMessageMatcher.Builder.class).getIfUnique(); |
|
||||||
} |
|
||||||
|
|
||||||
public static boolean usesPathPatterns() { |
|
||||||
return builder != null; |
|
||||||
} |
|
||||||
|
|
||||||
public static MessageMatcher<?> matcher(String destination) { |
|
||||||
return matcher(null, destination); |
|
||||||
} |
|
||||||
|
|
||||||
public static MessageMatcher<Object> matcher(SimpMessageType type, String destination) { |
|
||||||
return builder.matcher(type, destination); |
|
||||||
} |
|
||||||
|
|
||||||
private MessageMatcherFactory() { |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,176 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2002-2025 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.messaging.util.matcher; |
|
||||||
|
|
||||||
import java.util.Collections; |
|
||||||
import java.util.Map; |
|
||||||
|
|
||||||
import org.springframework.messaging.Message; |
|
||||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; |
|
||||||
import org.springframework.messaging.simp.SimpMessageType; |
|
||||||
import org.springframework.util.AntPathMatcher; |
|
||||||
import org.springframework.util.Assert; |
|
||||||
import org.springframework.util.PathMatcher; |
|
||||||
|
|
||||||
/** |
|
||||||
* <p> |
|
||||||
* MessageMatcher which compares a pre-defined pattern against the destination of a |
|
||||||
* {@link Message}. There is also support for optionally matching on a specified |
|
||||||
* {@link SimpMessageType}. |
|
||||||
* </p> |
|
||||||
* |
|
||||||
* @author Rob Winch |
|
||||||
* @since 4.0 |
|
||||||
* @deprecated use {@link PathPatternMessageMatcher} |
|
||||||
*/ |
|
||||||
@Deprecated |
|
||||||
public final class SimpDestinationMessageMatcher implements MessageMatcher<Object> { |
|
||||||
|
|
||||||
public static final MessageMatcher<Object> NULL_DESTINATION_MATCHER = (message) -> { |
|
||||||
String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders()); |
|
||||||
return destination == null; |
|
||||||
}; |
|
||||||
|
|
||||||
private final PathMatcher matcher; |
|
||||||
|
|
||||||
/** |
|
||||||
* The {@link MessageMatcher} that determines if the type matches. If the type was |
|
||||||
* null, this matcher will match every Message. |
|
||||||
*/ |
|
||||||
private final MessageMatcher<Object> messageTypeMatcher; |
|
||||||
|
|
||||||
private final String pattern; |
|
||||||
|
|
||||||
/** |
|
||||||
* <p> |
|
||||||
* Creates a new instance with the specified pattern, null {@link SimpMessageType} |
|
||||||
* (matches any type), and a {@link AntPathMatcher} created from the default |
|
||||||
* constructor. |
|
||||||
* |
|
||||||
* <p> |
|
||||||
* The mapping matches destinations despite the using the following rules: |
|
||||||
* |
|
||||||
* <ul> |
|
||||||
* <li>? matches one character</li> |
|
||||||
* <li>* matches zero or more characters</li> |
|
||||||
* <li>** matches zero or more 'directories' in a path</li> |
|
||||||
* </ul> |
|
||||||
* |
|
||||||
* <p> |
|
||||||
* Some examples: |
|
||||||
* |
|
||||||
* <ul> |
|
||||||
* <li>{@code com/t?st.jsp} - matches {@code com/test} but also {@code com/tast} or |
|
||||||
* {@code com/txst}</li> |
|
||||||
* <li>{@code com/*suffix} - matches all files ending in {@code suffix} in the |
|
||||||
* {@code com} directory</li> |
|
||||||
* <li>{@code com/**/test} - matches all destinations ending with {@code test} |
|
||||||
* underneath the {@code com} path</li> |
|
||||||
* </ul> |
|
||||||
* @param pattern the pattern to use |
|
||||||
*/ |
|
||||||
public SimpDestinationMessageMatcher(String pattern) { |
|
||||||
this(pattern, new AntPathMatcher()); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* <p> |
|
||||||
* Creates a new instance with the specified pattern and {@link PathMatcher}. |
|
||||||
* @param pattern the pattern to use |
|
||||||
* @param pathMatcher the {@link PathMatcher} to use. |
|
||||||
*/ |
|
||||||
public SimpDestinationMessageMatcher(String pattern, PathMatcher pathMatcher) { |
|
||||||
this(pattern, null, pathMatcher); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* <p> |
|
||||||
* Creates a new instance with the specified pattern, {@link SimpMessageType}, and |
|
||||||
* {@link PathMatcher}. |
|
||||||
* @param pattern the pattern to use |
|
||||||
* @param type the {@link SimpMessageType} to match on or null if any |
|
||||||
* {@link SimpMessageType} should be matched. |
|
||||||
* @param pathMatcher the {@link PathMatcher} to use. |
|
||||||
*/ |
|
||||||
private SimpDestinationMessageMatcher(String pattern, SimpMessageType type, PathMatcher pathMatcher) { |
|
||||||
Assert.notNull(pattern, "pattern cannot be null"); |
|
||||||
Assert.notNull(pathMatcher, "pathMatcher cannot be null"); |
|
||||||
Assert.isTrue(isTypeWithDestination(type), |
|
||||||
() -> "SimpMessageType " + type + " does not contain a destination and so cannot be matched on."); |
|
||||||
this.matcher = pathMatcher; |
|
||||||
this.messageTypeMatcher = (type != null) ? new SimpMessageTypeMatcher(type) : ANY_MESSAGE; |
|
||||||
this.pattern = pattern; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public boolean matches(Message<?> message) { |
|
||||||
if (!this.messageTypeMatcher.matches(message)) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders()); |
|
||||||
return destination != null && this.matcher.match(this.pattern, destination); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public MatchResult matcher(Message<?> message) { |
|
||||||
boolean match = matches(message); |
|
||||||
return (!match) ? MatchResult.notMatch() : MatchResult.match(extractPathVariables(message)); |
|
||||||
} |
|
||||||
|
|
||||||
public Map<String, String> extractPathVariables(Message<?> message) { |
|
||||||
final String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders()); |
|
||||||
return (destination != null) ? this.matcher.extractUriTemplateVariables(this.pattern, destination) |
|
||||||
: Collections.emptyMap(); |
|
||||||
} |
|
||||||
|
|
||||||
public MessageMatcher<Object> getMessageTypeMatcher() { |
|
||||||
return this.messageTypeMatcher; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "SimpDestinationMessageMatcher [matcher=" + this.matcher + ", messageTypeMatcher=" |
|
||||||
+ this.messageTypeMatcher + ", pattern=" + this.pattern + "]"; |
|
||||||
} |
|
||||||
|
|
||||||
private boolean isTypeWithDestination(SimpMessageType type) { |
|
||||||
return type == null || SimpMessageType.MESSAGE.equals(type) || SimpMessageType.SUBSCRIBE.equals(type); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* <p> |
|
||||||
* Creates a new instance with the specified pattern, |
|
||||||
* {@code SimpMessageType.SUBSCRIBE}, and {@link PathMatcher}. |
|
||||||
* @param pattern the pattern to use |
|
||||||
* @param matcher the {@link PathMatcher} to use. |
|
||||||
*/ |
|
||||||
public static SimpDestinationMessageMatcher createSubscribeMatcher(String pattern, PathMatcher matcher) { |
|
||||||
return new SimpDestinationMessageMatcher(pattern, SimpMessageType.SUBSCRIBE, matcher); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* <p> |
|
||||||
* Creates a new instance with the specified pattern, {@code SimpMessageType.MESSAGE}, |
|
||||||
* and {@link PathMatcher}. |
|
||||||
* @param pattern the pattern to use |
|
||||||
* @param matcher the {@link PathMatcher} to use. |
|
||||||
*/ |
|
||||||
public static SimpDestinationMessageMatcher createMessageMatcher(String pattern, PathMatcher matcher) { |
|
||||||
return new SimpDestinationMessageMatcher(pattern, SimpMessageType.MESSAGE, matcher); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -1,141 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2002-2025 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.messaging.util.matcher; |
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach; |
|
||||||
import org.junit.jupiter.api.Test; |
|
||||||
|
|
||||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; |
|
||||||
import org.springframework.messaging.simp.SimpMessageType; |
|
||||||
import org.springframework.messaging.support.MessageBuilder; |
|
||||||
import org.springframework.util.AntPathMatcher; |
|
||||||
import org.springframework.util.PathMatcher; |
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat; |
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
|
||||||
|
|
||||||
public class SimpDestinationMessageMatcherTests { |
|
||||||
|
|
||||||
MessageBuilder<String> messageBuilder; |
|
||||||
|
|
||||||
SimpDestinationMessageMatcher matcher; |
|
||||||
|
|
||||||
PathMatcher pathMatcher; |
|
||||||
|
|
||||||
@BeforeEach |
|
||||||
public void setup() { |
|
||||||
this.messageBuilder = MessageBuilder.withPayload("M"); |
|
||||||
this.matcher = new SimpDestinationMessageMatcher("/**"); |
|
||||||
this.pathMatcher = new AntPathMatcher(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void constructorPatternNull() { |
|
||||||
assertThatIllegalArgumentException().isThrownBy(() -> new SimpDestinationMessageMatcher(null)); |
|
||||||
} |
|
||||||
|
|
||||||
public void constructorOnlyPathNoError() { |
|
||||||
new SimpDestinationMessageMatcher("/path"); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void matchesDoesNotMatchNullDestination() { |
|
||||||
assertThat(this.matcher.matches(this.messageBuilder.build())).isFalse(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void matchesAllWithDestination() { |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1"); |
|
||||||
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void matchesSpecificWithDestination() { |
|
||||||
this.matcher = new SimpDestinationMessageMatcher("/destination/1"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1"); |
|
||||||
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void matchesFalseWithDestination() { |
|
||||||
this.matcher = new SimpDestinationMessageMatcher("/nomatch"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1"); |
|
||||||
assertThat(this.matcher.matches(this.messageBuilder.build())).isFalse(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void matchesFalseMessageTypeNotDisconnectType() { |
|
||||||
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.DISCONNECT); |
|
||||||
assertThat(this.matcher.matches(this.messageBuilder.build())).isFalse(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void matchesTrueMessageType() { |
|
||||||
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE); |
|
||||||
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void matchesTrueSubscribeType() { |
|
||||||
this.matcher = SimpDestinationMessageMatcher.createSubscribeMatcher("/match", this.pathMatcher); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.SUBSCRIBE); |
|
||||||
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void matchesNullMessageType() { |
|
||||||
this.matcher = new SimpDestinationMessageMatcher("/match"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/match"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE); |
|
||||||
assertThat(this.matcher.matches(this.messageBuilder.build())).isTrue(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void extractPathVariablesFromDestination() { |
|
||||||
this.matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE); |
|
||||||
assertThat(this.matcher.extractPathVariables(this.messageBuilder.build())).containsEntry("topic", "someTopic"); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void extractedVariablesAreEmptyInNullDestination() { |
|
||||||
this.matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**"); |
|
||||||
assertThat(this.matcher.extractPathVariables(this.messageBuilder.build())).isEmpty(); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void typeConstructorParameterIsTransmitted() { |
|
||||||
this.matcher = SimpDestinationMessageMatcher.createMessageMatcher("/match", this.pathMatcher); |
|
||||||
MessageMatcher<Object> expectedTypeMatcher = new SimpMessageTypeMatcher(SimpMessageType.MESSAGE); |
|
||||||
assertThat(this.matcher.getMessageTypeMatcher()).isEqualTo(expectedTypeMatcher); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void extractPathVariablesWhenNoMatchThenIllegalState() { |
|
||||||
this.matcher = new SimpDestinationMessageMatcher("/nomatch"); |
|
||||||
this.messageBuilder.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/destination/1"); |
|
||||||
assertThatIllegalStateException() |
|
||||||
.isThrownBy(() -> this.matcher.extractPathVariables(this.messageBuilder.build())); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue