17
17
package org .springframework .security .messaging .access .intercept ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Arrays ;
20
21
import java .util .List ;
21
22
import java .util .Map ;
22
23
import java .util .function .Supplier ;
34
35
import org .springframework .security .authorization .SingleResultAuthorizationManager ;
35
36
import org .springframework .security .core .Authentication ;
36
37
import org .springframework .security .messaging .util .matcher .MessageMatcher ;
38
+ import org .springframework .security .messaging .util .matcher .MessageMatcherFactory ;
39
+ import org .springframework .security .messaging .util .matcher .PathPatternMessageMatcher ;
37
40
import org .springframework .security .messaging .util .matcher .SimpDestinationMessageMatcher ;
38
41
import org .springframework .security .messaging .util .matcher .SimpMessageTypeMatcher ;
39
42
import org .springframework .util .AntPathMatcher ;
40
43
import org .springframework .util .Assert ;
44
+ import org .springframework .util .CollectionUtils ;
41
45
import org .springframework .util .PathMatcher ;
42
46
import org .springframework .util .function .SingletonSupplier ;
43
47
@@ -85,15 +89,17 @@ public AuthorizationDecision check(Supplier<Authentication> authentication, Mess
85
89
}
86
90
87
91
private MessageAuthorizationContext <?> authorizationContext (MessageMatcher <?> matcher , Message <?> message ) {
88
- if (!matcher .matches ((Message ) message )) {
92
+ MessageMatcher .MatchResult matchResult = matcher .matcher ((Message ) message );
93
+ if (!matchResult .isMatch ()) {
89
94
return null ;
90
95
}
91
- if (matcher instanceof SimpDestinationMessageMatcher simp ) {
92
- return new MessageAuthorizationContext <>(message , simp .extractPathVariables (message ));
96
+
97
+ if (!CollectionUtils .isEmpty (matchResult .getVariables ())) {
98
+ return new MessageAuthorizationContext <>(message , matchResult .getVariables ());
93
99
}
94
- if ( matcher instanceof Builder . LazySimpDestinationMessageMatcher ) {
95
- Builder . LazySimpDestinationMessageMatcher path = ( Builder .LazySimpDestinationMessageMatcher ) matcher ;
96
- return new MessageAuthorizationContext <>(message , path .extractPathVariables (message ));
100
+
101
+ if ( matcher instanceof Builder .LazySimpDestinationMessageMatcher pathMatcher ) {
102
+ return new MessageAuthorizationContext <>(message , pathMatcher .extractPathVariables (message ));
97
103
}
98
104
return new MessageAuthorizationContext <>(message );
99
105
}
@@ -113,6 +119,7 @@ public static final class Builder {
113
119
114
120
private final List <Entry <AuthorizationManager <MessageAuthorizationContext <?>>>> mappings = new ArrayList <>();
115
121
122
+ @ Deprecated
116
123
private Supplier <PathMatcher > pathMatcher = AntPathMatcher ::new ;
117
124
118
125
public Builder () {
@@ -133,11 +140,11 @@ public Builder.Constraint anyMessage() {
133
140
* @return the Expression to associate
134
141
*/
135
142
public Builder .Constraint nullDestMatcher () {
136
- return matchers (SimpDestinationMessageMatcher .NULL_DESTINATION_MATCHER );
143
+ return matchers (PathPatternMessageMatcher .NULL_DESTINATION_MATCHER );
137
144
}
138
145
139
146
/**
140
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher } instances.
147
+ * Maps a {@link List} of {@link SimpMessageTypeMatcher } instances.
141
148
* @param typesToMatch the {@link SimpMessageType} instance to match on
142
149
* @return the {@link Builder.Constraint} associated to the matchers.
143
150
*/
@@ -151,56 +158,58 @@ public Builder.Constraint simpTypeMatchers(SimpMessageType... typesToMatch) {
151
158
}
152
159
153
160
/**
154
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances without
155
- * regard to the {@link SimpMessageType}. If no destination is found on the
156
- * Message, then the Matcher returns false.
157
- * @param patterns the patterns to create
158
- * {@link org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher}
159
- * from.
161
+ * Maps a {@link List} of {@link SimpDestinationMessageMatcher} (or
162
+ * {@link PathPatternMessageMatcher} if the application has configured a
163
+ * {@link org.springframework.security.messaging.util.matcher.PathPatternMessageMatcherBuilderFactoryBean})
164
+ * instances without regard to the {@link SimpMessageType}. If no destination is
165
+ * found on the Message, then the Matcher returns false.
166
+ * @param patterns the patterns to create {@code MessageMatcher}s from.
160
167
*/
161
168
public Builder .Constraint simpDestMatchers (String ... patterns ) {
162
169
return simpDestMatchers (null , patterns );
163
170
}
164
171
165
172
/**
166
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances that
167
- * match on {@code SimpMessageType.MESSAGE}. If no destination is found on the
168
- * Message, then the Matcher returns false.
169
- * @param patterns the patterns to create
170
- * {@link org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher}
171
- * from.
173
+ * Maps a {@link List} of {@link SimpDestinationMessageMatcher} (or
174
+ * {@link PathPatternMessageMatcher} if the application has configured a
175
+ * {@link org.springframework.security.messaging.util.matcher.PathPatternMessageMatcherBuilderFactoryBean})
176
+ * instances that match on {@code SimpMessageType.MESSAGE}. If no destination is
177
+ * found on the Message, then the Matcher returns false.
178
+ * @param patterns the patterns to create {@code MessageMatcher}s from.
172
179
*/
173
180
public Builder .Constraint simpMessageDestMatchers (String ... patterns ) {
174
181
return simpDestMatchers (SimpMessageType .MESSAGE , patterns );
175
182
}
176
183
177
184
/**
178
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances that
179
- * match on {@code SimpMessageType.SUBSCRIBE}. If no destination is found on the
180
- * Message, then the Matcher returns false.
181
- * @param patterns the patterns to create
182
- * {@link org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher}
183
- * from.
185
+ * Maps a {@link List} of {@link SimpDestinationMessageMatcher} (or
186
+ * {@link PathPatternMessageMatcher} if the application has configured a
187
+ * {@link org.springframework.security.messaging.util.matcher.PathPatternMessageMatcherBuilderFactoryBean})
188
+ * instances that match on {@code SimpMessageType.SUBSCRIBE}. If no destination is
189
+ * found on the Message, then the Matcher returns false.
190
+ * @param patterns the patterns to create {@code MessageMatcher}s from.
184
191
*/
185
192
public Builder .Constraint simpSubscribeDestMatchers (String ... patterns ) {
186
193
return simpDestMatchers (SimpMessageType .SUBSCRIBE , patterns );
187
194
}
188
195
189
196
/**
190
- * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances. If no
191
- * destination is found on the Message, then the Matcher returns false.
197
+ * Maps a {@link List} of {@link SimpDestinationMessageMatcher} instances, or
198
+ * {@link PathPatternMessageMatcher} if the application has configured a
199
+ * {@link org.springframework.security.messaging.util.matcher.PathPatternMessageMatcherBuilderFactoryBean}.
200
+ * If no destination is found on the Message, then the Matcher returns false.
192
201
* @param type the {@link SimpMessageType} to match on. If null, the
193
202
* {@link SimpMessageType} is not considered for matching.
194
- * @param patterns the patterns to create
195
- * {@link org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher}
196
- * from.
203
+ * @param patterns the patterns to create {@code MessageMatcher}s from.
197
204
* @return the {@link Builder.Constraint} that is associated to the
198
205
* {@link MessageMatcher}
199
206
*/
200
207
private Builder .Constraint simpDestMatchers (SimpMessageType type , String ... patterns ) {
201
208
List <MessageMatcher <?>> matchers = new ArrayList <>(patterns .length );
202
209
for (String pattern : patterns ) {
203
- MessageMatcher <Object > matcher = new LazySimpDestinationMessageMatcher (pattern , type );
210
+ MessageMatcher <Object > matcher = MessageMatcherFactory .usesPathPatterns ()
211
+ ? MessageMatcherFactory .matcher (pattern , type )
212
+ : new LazySimpDestinationMessageMatcher (pattern , type );
204
213
matchers .add (matcher );
205
214
}
206
215
return new Builder .Constraint (matchers );
@@ -212,7 +221,9 @@ private Builder.Constraint simpDestMatchers(SimpMessageType type, String... patt
212
221
* constructor of {@link AntPathMatcher}.
213
222
* @param pathMatcher the {@link PathMatcher} to use. Cannot be null.
214
223
* @return the {@link Builder} for further customization.
224
+ * @deprecated
215
225
*/
226
+ @ Deprecated
216
227
public Builder simpDestPathMatcher (PathMatcher pathMatcher ) {
217
228
Assert .notNull (pathMatcher , "pathMatcher cannot be null" );
218
229
this .pathMatcher = () -> pathMatcher ;
@@ -225,7 +236,9 @@ public Builder simpDestPathMatcher(PathMatcher pathMatcher) {
225
236
* computation or lookup of the {@link PathMatcher}.
226
237
* @param pathMatcher the {@link PathMatcher} to use. Cannot be null.
227
238
* @return the {@link Builder} for further customization.
239
+ * @deprecated
228
240
*/
241
+ @ Deprecated
229
242
public Builder simpDestPathMatcher (Supplier <PathMatcher > pathMatcher ) {
230
243
Assert .notNull (pathMatcher , "pathMatcher cannot be null" );
231
244
this .pathMatcher = pathMatcher ;
@@ -241,9 +254,7 @@ public Builder simpDestPathMatcher(Supplier<PathMatcher> pathMatcher) {
241
254
*/
242
255
public Builder .Constraint matchers (MessageMatcher <?>... matchers ) {
243
256
List <MessageMatcher <?>> builders = new ArrayList <>(matchers .length );
244
- for (MessageMatcher <?> matcher : matchers ) {
245
- builders .add (matcher );
246
- }
257
+ builders .addAll (Arrays .asList (matchers ));
247
258
return new Builder .Constraint (builders );
248
259
}
249
260
@@ -382,6 +393,7 @@ public Builder access(AuthorizationManager<MessageAuthorizationContext<?>> autho
382
393
383
394
}
384
395
396
+ @ Deprecated
385
397
private final class LazySimpDestinationMessageMatcher implements MessageMatcher <Object > {
386
398
387
399
private final Supplier <SimpDestinationMessageMatcher > delegate ;
@@ -421,7 +433,7 @@ private static final class Entry<T> {
421
433
422
434
private final T entry ;
423
435
424
- Entry (MessageMatcher requestMatcher , T entry ) {
436
+ Entry (MessageMatcher <?> requestMatcher , T entry ) {
425
437
this .messageMatcher = requestMatcher ;
426
438
this .entry = entry ;
427
439
}
0 commit comments