Skip to content

Add DestinationPathPatternMessageMatcher #16635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* 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.
Expand All @@ -21,6 +21,7 @@
import org.springframework.context.annotation.Scope;
import org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler;
import org.springframework.security.messaging.access.intercept.MessageMatcherDelegatingAuthorizationManager;
import org.springframework.security.messaging.util.matcher.MessageMatcherFactory;
import org.springframework.util.AntPathMatcher;

final class MessageMatcherAuthorizationManagerConfiguration {
Expand All @@ -29,6 +30,7 @@ final class MessageMatcherAuthorizationManagerConfiguration {
@Scope("prototype")
MessageMatcherDelegatingAuthorizationManager.Builder messageAuthorizationManagerBuilder(
ApplicationContext context) {
MessageMatcherFactory.setApplicationContext(context);
return MessageMatcherDelegatingAuthorizationManager.builder()
.simpDestPathMatcher(
() -> (context.getBeanNamesForType(SimpAnnotationMethodMessageHandler.class).length > 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.config.web.messaging;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.security.messaging.access.intercept.MessageMatcherDelegatingAuthorizationManager;
import org.springframework.security.messaging.util.matcher.PathPatternMessageMatcher;

/**
* Use this factory bean to configure the {@link PathPatternMessageMatcher.Builder} bean
* used to create request matchers in {@link MessageMatcherDelegatingAuthorizationManager}
* and other parts of the DSL.
*
* @author Pat McCusker
* @since 6.5
*/
public final class PathPatternMessageMatcherBuilderFactoryBean
implements FactoryBean<PathPatternMessageMatcher.Builder> {

@Override
public PathPatternMessageMatcher.Builder getObject() throws Exception {
return PathPatternMessageMatcher.withDefaults();
}

@Override
public Class<?> getObjectType() {
return PathPatternMessageMatcher.Builder.class;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.apache.commons.logging.Log;
Expand All @@ -34,10 +33,13 @@
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;

Expand Down Expand Up @@ -85,16 +87,15 @@ public AuthorizationDecision check(Supplier<Authentication> authentication, Mess
}

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 (matcher instanceof Builder.LazySimpDestinationMessageMatcher) {
Builder.LazySimpDestinationMessageMatcher path = (Builder.LazySimpDestinationMessageMatcher) matcher;
return new MessageAuthorizationContext<>(message, path.extractPathVariables(message));

if (!CollectionUtils.isEmpty(matchResult.getVariables())) {
return new MessageAuthorizationContext<>(message, matchResult.getVariables());
}

return new MessageAuthorizationContext<>(message);
}

Expand All @@ -113,6 +114,7 @@ public static final class Builder {

private final List<Entry<AuthorizationManager<MessageAuthorizationContext<?>>>> mappings = new ArrayList<>();

@Deprecated
private Supplier<PathMatcher> pathMatcher = AntPathMatcher::new;

public Builder() {
Expand All @@ -133,11 +135,11 @@ public Builder.Constraint anyMessage() {
* @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.
*/
Expand All @@ -151,56 +153,58 @@ public Builder.Constraint simpTypeMatchers(SimpMessageType... typesToMatch) {
}

/**
* 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(type, pattern)
: new LazySimpDestinationMessageMatcher(pattern, type);
matchers.add(matcher);
}
return new Builder.Constraint(matchers);
Expand All @@ -212,7 +216,9 @@ private Builder.Constraint simpDestMatchers(SimpMessageType type, String... patt
* 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;
Expand All @@ -225,7 +231,9 @@ public Builder simpDestPathMatcher(PathMatcher pathMatcher) {
* 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;
Expand Down Expand Up @@ -382,6 +390,7 @@ public Builder access(AuthorizationManager<MessageAuthorizationContext<?>> autho

}

@Deprecated
private final class LazySimpDestinationMessageMatcher implements MessageMatcher<Object> {

private final Supplier<SimpDestinationMessageMatcher> delegate;
Expand All @@ -407,8 +416,9 @@ public boolean matches(Message<?> message) {
return this.delegate.get().matches(message);
}

Map<String, String> extractPathVariables(Message<?> message) {
return this.delegate.get().extractPathVariables(message);
@Override
public MatchResult matcher(Message<?> message) {
return this.delegate.get().matcher(message);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* 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.
Expand All @@ -16,6 +16,9 @@

package org.springframework.security.messaging.util.matcher;

import java.util.Collections;
import java.util.Map;

import org.springframework.messaging.Message;

/**
Expand Down Expand Up @@ -50,4 +53,77 @@ public String toString() {
*/
boolean matches(Message<? extends T> message);

/**
* Returns a {@link MatchResult} for this {@code MessageMatcher}. The default
* implementation returns {@link Collections#emptyMap()} when
* {@link MatchResult#getVariables()} is invoked.
* @return the {@code MatchResult} from comparing this {@code MessageMatcher} against
* the {@code Message}
* @since 6.5
*/
default MatchResult matcher(Message<? extends T> message) {
boolean match = matches(message);
return new MatchResult(match, Collections.emptyMap());
}

/**
* The result of matching against a {@code Message} contains the status, true or
* false, of the match and if present, any variables extracted from the match
*
* @since 6.5
*/
class MatchResult {

private final boolean match;

private final Map<String, String> variables;

MatchResult(boolean match, Map<String, String> variables) {
this.match = match;
this.variables = variables;
}

/**
* Return whether the comparison against the {@code Message} produced a successful
* match
*/
public boolean isMatch() {
return this.match;
}

/**
* Returns the extracted variable values where the key is the variable name and
* the value is the variable value
* @return a map containing key-value pairs representing extracted variable names
* and variable values
*/
public Map<String, String> getVariables() {
return this.variables;
}

/**
* Creates an instance of {@link MatchResult} that is a match with no variables
*/
public static MatchResult match() {
return new MatchResult(true, Collections.emptyMap());
}

/**
* Creates an instance of {@link MatchResult} that is a match with the specified
* variables
*/
public static MatchResult match(Map<String, String> variables) {
return new MatchResult(true, variables);
}

/**
* Creates an instance of {@link MatchResult} that is not a match.
* @return a {@code MatchResult} with match set to false
*/
public static MatchResult notMatch() {
return new MatchResult(false, Collections.emptyMap());
}

}

}
Loading