|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
34 | 34 | import org.springframework.security.core.Authentication;
|
35 | 35 | import org.springframework.security.core.authority.AuthorityUtils;
|
36 | 36 | import org.springframework.security.core.context.SecurityContextHolder;
|
| 37 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
| 38 | +import org.springframework.security.core.context.SecurityContextImpl; |
37 | 39 |
|
38 | 40 | import static org.assertj.core.api.Assertions.assertThat;
|
39 | 41 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 42 | +import static org.mockito.Mockito.spy; |
| 43 | +import static org.mockito.Mockito.verify; |
40 | 44 |
|
41 | 45 | @ExtendWith(MockitoExtension.class)
|
42 | 46 | public class SecurityContextChannelInterceptorTests {
|
@@ -94,6 +98,17 @@ public void preSendUserSet() {
|
94 | 98 | assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
|
95 | 99 | }
|
96 | 100 |
|
| 101 | + @Test |
| 102 | + public void preSendWhenCustomSecurityContextHolderStrategyThenUserSet() { |
| 103 | + SecurityContextHolderStrategy strategy = spy(SecurityContextHolder.getContextHolderStrategy()); |
| 104 | + strategy.setContext(new SecurityContextImpl(this.authentication)); |
| 105 | + this.interceptor.setSecurityContextHolderStrategy(strategy); |
| 106 | + this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication); |
| 107 | + this.interceptor.preSend(this.messageBuilder.build(), this.channel); |
| 108 | + verify(strategy).getContext(); |
| 109 | + assertThat(strategy.getContext().getAuthentication()).isSameAs(this.authentication); |
| 110 | + } |
| 111 | + |
97 | 112 | @Test
|
98 | 113 | public void setAnonymousAuthenticationNull() {
|
99 | 114 | assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.setAnonymousAuthentication(null));
|
@@ -143,13 +158,34 @@ public void afterSendCompletionNullAuthentication() {
|
143 | 158 | assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
|
144 | 159 | }
|
145 | 160 |
|
| 161 | + @Test |
| 162 | + public void afterSendCompletionWhenCustomSecurityContextHolderStrategyThenNullAuthentication() { |
| 163 | + SecurityContextHolderStrategy strategy = spy(SecurityContextHolder.getContextHolderStrategy()); |
| 164 | + strategy.setContext(new SecurityContextImpl(this.authentication)); |
| 165 | + this.interceptor.setSecurityContextHolderStrategy(strategy); |
| 166 | + this.interceptor.afterSendCompletion(this.messageBuilder.build(), this.channel, true, null); |
| 167 | + verify(strategy).clearContext(); |
| 168 | + assertThat(strategy.getContext().getAuthentication()).isNull(); |
| 169 | + } |
| 170 | + |
146 | 171 | @Test
|
147 | 172 | public void beforeHandleUserSet() {
|
148 | 173 | this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication);
|
149 | 174 | this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler);
|
150 | 175 | assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.authentication);
|
151 | 176 | }
|
152 | 177 |
|
| 178 | + @Test |
| 179 | + public void beforeHandleWhenCustomSecurityContextHolderStrategyThenUserSet() { |
| 180 | + SecurityContextHolderStrategy strategy = spy(SecurityContextHolder.getContextHolderStrategy()); |
| 181 | + strategy.setContext(new SecurityContextImpl(this.authentication)); |
| 182 | + this.interceptor.setSecurityContextHolderStrategy(strategy); |
| 183 | + this.messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, this.authentication); |
| 184 | + this.interceptor.beforeHandle(this.messageBuilder.build(), this.channel, this.handler); |
| 185 | + verify(strategy).getContext(); |
| 186 | + assertThat(strategy.getContext().getAuthentication()).isSameAs(this.authentication); |
| 187 | + } |
| 188 | + |
153 | 189 | // SEC-2845
|
154 | 190 | @Test
|
155 | 191 | public void beforeHandleUserNotAuthentication() {
|
@@ -178,6 +214,15 @@ public void afterMessageHandled() {
|
178 | 214 | assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
|
179 | 215 | }
|
180 | 216 |
|
| 217 | + @Test |
| 218 | + public void afterMessageHandledWhenCustomSecurityContextHolderStrategyThenUses() { |
| 219 | + SecurityContextHolderStrategy strategy = spy(SecurityContextHolder.getContextHolderStrategy()); |
| 220 | + strategy.setContext(new SecurityContextImpl(this.authentication)); |
| 221 | + this.interceptor.setSecurityContextHolderStrategy(strategy); |
| 222 | + this.interceptor.afterMessageHandled(this.messageBuilder.build(), this.channel, this.handler, null); |
| 223 | + verify(strategy).clearContext(); |
| 224 | + } |
| 225 | + |
181 | 226 | // SEC-2829
|
182 | 227 | @Test
|
183 | 228 | public void restoresOriginalContext() {
|
|
0 commit comments