|
1 | 1 | /* |
2 | | - * Copyright 2002-2015 the original author or authors. |
3 | 2 | * |
4 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | 4 | * use this file except in compliance with the License. You may obtain a copy of |
|
17 | 16 |
|
18 | 17 | import org.junit.After; |
19 | 18 | import org.junit.Before; |
20 | | - |
21 | 19 | import org.junit.Test; |
22 | 20 | import org.springframework.context.annotation.Bean; |
23 | 21 | import org.springframework.context.annotation.Configuration; |
|
46 | 44 | import org.springframework.security.web.csrf.MissingCsrfTokenException; |
47 | 45 | import org.springframework.stereotype.Controller; |
48 | 46 | import org.springframework.test.util.ReflectionTestUtils; |
| 47 | +import org.springframework.util.AntPathMatcher; |
49 | 48 | import org.springframework.web.HttpRequestHandler; |
50 | 49 | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
51 | 50 | import org.springframework.web.servlet.HandlerMapping; |
|
59 | 58 | import org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession; |
60 | 59 |
|
61 | 60 | import javax.servlet.http.HttpServletRequest; |
| 61 | + |
62 | 62 | import java.util.HashMap; |
63 | 63 | import java.util.Map; |
64 | 64 |
|
@@ -232,6 +232,58 @@ public void messagesConnectWebSocketUseCsrfTokenHandshakeInterceptor() |
232 | 232 | assertHandshake(request); |
233 | 233 | } |
234 | 234 |
|
| 235 | + @Test |
| 236 | + public void msmsRegistryCustomPatternMatcher() |
| 237 | + throws Exception { |
| 238 | + loadConfig(MsmsRegistryCustomPatternMatcherConfig.class); |
| 239 | + |
| 240 | + clientInboundChannel().send(message("/app/a.b")); |
| 241 | + |
| 242 | + try { |
| 243 | + clientInboundChannel().send(message("/app/a.b.c")); |
| 244 | + fail("Expected Exception"); |
| 245 | + } |
| 246 | + catch (MessageDeliveryException expected) { |
| 247 | + assertThat(expected.getCause()).isInstanceOf(AccessDeniedException.class); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + @Configuration |
| 252 | + @EnableWebSocketMessageBroker |
| 253 | + @Import(SyncExecutorConfig.class) |
| 254 | + static class MsmsRegistryCustomPatternMatcherConfig extends |
| 255 | + AbstractSecurityWebSocketMessageBrokerConfigurer { |
| 256 | + |
| 257 | + public void registerStompEndpoints(StompEndpointRegistry registry) { |
| 258 | + registry.addEndpoint("/other").setHandshakeHandler(testHandshakeHandler()) |
| 259 | + .withSockJS().setInterceptors(new HttpSessionHandshakeInterceptor()); |
| 260 | + |
| 261 | + registry.addEndpoint("/chat").setHandshakeHandler(testHandshakeHandler()) |
| 262 | + .withSockJS().setInterceptors(new HttpSessionHandshakeInterceptor()); |
| 263 | + } |
| 264 | + |
| 265 | + // @formatter:off |
| 266 | + @Override |
| 267 | + protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { |
| 268 | + messages |
| 269 | + .simpDestMatchers("/app/a.*").permitAll() |
| 270 | + .anyMessage().denyAll(); |
| 271 | + } |
| 272 | + // @formatter:on |
| 273 | + |
| 274 | + @Override |
| 275 | + public void configureMessageBroker(MessageBrokerRegistry registry) { |
| 276 | + registry.setPathMatcher(new AntPathMatcher(".")); |
| 277 | + registry.enableSimpleBroker("/queue/", "/topic/"); |
| 278 | + registry.setApplicationDestinationPrefixes("/app"); |
| 279 | + } |
| 280 | + |
| 281 | + @Bean |
| 282 | + public TestHandshakeHandler testHandshakeHandler() { |
| 283 | + return new TestHandshakeHandler(); |
| 284 | + } |
| 285 | + } |
| 286 | + |
235 | 287 | private void assertHandshake(HttpServletRequest request) { |
236 | 288 | TestHandshakeHandler handshakeHandler = context |
237 | 289 | .getBean(TestHandshakeHandler.class); |
@@ -358,10 +410,14 @@ public void registerStompEndpoints(StompEndpointRegistry registry) { |
358 | 410 | .withSockJS().setInterceptors(new HttpSessionHandshakeInterceptor()); |
359 | 411 | } |
360 | 412 |
|
| 413 | + // @formatter:off |
361 | 414 | @Override |
362 | 415 | protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { |
363 | | - messages.simpDestMatchers("/permitAll/**").permitAll().anyMessage().denyAll(); |
| 416 | + messages |
| 417 | + .simpDestMatchers("/permitAll/**").permitAll() |
| 418 | + .anyMessage().denyAll(); |
364 | 419 | } |
| 420 | + // @formatter:on |
365 | 421 |
|
366 | 422 | @Override |
367 | 423 | public void configureMessageBroker(MessageBrokerRegistry registry) { |
@@ -431,10 +487,14 @@ public void registerStompEndpoints(StompEndpointRegistry registry) { |
431 | 487 | .addInterceptors(new HttpSessionHandshakeInterceptor()); |
432 | 488 | } |
433 | 489 |
|
| 490 | + // @formatter:off |
434 | 491 | @Override |
435 | 492 | protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { |
436 | | - messages.simpDestMatchers("/permitAll/**").permitAll().anyMessage().denyAll(); |
| 493 | + messages |
| 494 | + .simpDestMatchers("/permitAll/**").permitAll() |
| 495 | + .anyMessage().denyAll(); |
437 | 496 | } |
| 497 | + // @formatter:on |
438 | 498 |
|
439 | 499 | @Bean |
440 | 500 | public TestHandshakeHandler testHandshakeHandler() { |
|
0 commit comments