|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.websocket;
|
18 | 18 |
|
| 19 | +import java.lang.annotation.ElementType; |
| 20 | +import java.lang.annotation.Retention; |
| 21 | +import java.lang.annotation.RetentionPolicy; |
| 22 | +import java.lang.annotation.Target; |
19 | 23 | import java.util.HashMap;
|
20 | 24 | import java.util.Map;
|
21 | 25 | import java.util.function.Supplier;
|
|
47 | 51 | import org.springframework.messaging.support.GenericMessage;
|
48 | 52 | import org.springframework.security.access.AccessDeniedException;
|
49 | 53 | import org.springframework.security.access.expression.SecurityExpressionOperations;
|
| 54 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
50 | 55 | import org.springframework.security.authorization.AuthorizationDecision;
|
51 | 56 | import org.springframework.security.authorization.AuthorizationManager;
|
52 | 57 | import org.springframework.security.config.test.SpringTestContext;
|
|
55 | 60 | import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
56 | 61 | import org.springframework.security.core.context.SecurityContextHolder;
|
57 | 62 | import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
| 63 | +import org.springframework.security.core.context.SecurityContextImpl; |
58 | 64 | import org.springframework.security.messaging.access.expression.DefaultMessageSecurityExpressionHandler;
|
59 | 65 | import org.springframework.security.messaging.access.expression.MessageSecurityExpressionRoot;
|
60 | 66 | import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners;
|
@@ -376,6 +382,24 @@ public void sendWhenNoIdMessageThenAuthenticationPrincipalResolved() {
|
376 | 382 | assertThat(this.messageController.username).isEqualTo("anonymous");
|
377 | 383 | }
|
378 | 384 |
|
| 385 | + @Test |
| 386 | + public void sendMessageWhenMetaAnnotationThenAuthenticationPrincipalResolved() { |
| 387 | + this.spring.configLocations(xml("SyncConfig")).autowire(); |
| 388 | + Authentication harold = new TestingAuthenticationToken("harold", "password", "ROLE_USER"); |
| 389 | + try { |
| 390 | + getSecurityContextHolderStrategy().setContext(new SecurityContextImpl(harold)); |
| 391 | + this.clientInboundChannel.send(message("/hi")); |
| 392 | + assertThat(this.spring.getContext().getBean(MessageController.class).message).isEqualTo("Hi, Harold!"); |
| 393 | + Authentication user = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
| 394 | + getSecurityContextHolderStrategy().setContext(new SecurityContextImpl(user)); |
| 395 | + this.clientInboundChannel.send(message("/hi")); |
| 396 | + assertThat(this.spring.getContext().getBean(MessageController.class).message).isEqualTo("Hi, Stranger!"); |
| 397 | + } |
| 398 | + finally { |
| 399 | + getSecurityContextHolderStrategy().clearContext(); |
| 400 | + } |
| 401 | + } |
| 402 | + |
379 | 403 | @Test
|
380 | 404 | public void requestWhenConnectMessageThenUsesCsrfTokenHandshakeInterceptor() throws Exception {
|
381 | 405 | this.spring.configLocations(xml("SyncConfig")).autowire();
|
@@ -553,16 +577,32 @@ public boolean isGenerated() {
|
553 | 577 |
|
554 | 578 | }
|
555 | 579 |
|
| 580 | + @Retention(RetentionPolicy.RUNTIME) |
| 581 | + @Target(ElementType.PARAMETER) |
| 582 | + @AuthenticationPrincipal(expression = "#this.equals('{value}')") |
| 583 | + @interface IsUser { |
| 584 | + |
| 585 | + String value() default "user"; |
| 586 | + |
| 587 | + } |
| 588 | + |
556 | 589 | @Controller
|
557 | 590 | static class MessageController {
|
558 | 591 |
|
559 | 592 | String username;
|
560 | 593 |
|
| 594 | + String message; |
| 595 | + |
561 | 596 | @MessageMapping("/message")
|
562 | 597 | void authentication(@AuthenticationPrincipal String username) {
|
563 | 598 | this.username = username;
|
564 | 599 | }
|
565 | 600 |
|
| 601 | + @MessageMapping("/hi") |
| 602 | + void sayHello(@IsUser("harold") boolean isHarold) { |
| 603 | + this.message = isHarold ? "Hi, Harold!" : "Hi, Stranger!"; |
| 604 | + } |
| 605 | + |
566 | 606 | }
|
567 | 607 |
|
568 | 608 | @Controller
|
|
0 commit comments