|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.core.context; |
| 18 | + |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | + |
| 26 | +import org.springframework.core.task.SimpleAsyncTaskExecutor; |
| 27 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link ReactiveSecurityContextHolderThreadLocalAccessor}. |
| 34 | + * |
| 35 | + * @author Steve Riesenberg |
| 36 | + */ |
| 37 | +public class ReactiveSecurityContextHolderThreadLocalAccessorTests { |
| 38 | + |
| 39 | + private ReactiveSecurityContextHolderThreadLocalAccessor threadLocalAccessor; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + public void setUp() { |
| 43 | + this.threadLocalAccessor = new ReactiveSecurityContextHolderThreadLocalAccessor(); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterEach |
| 47 | + public void tearDown() { |
| 48 | + this.threadLocalAccessor.setValue(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void keyAlwaysReturnsSecurityContextClass() { |
| 53 | + assertThat(this.threadLocalAccessor.key()).isEqualTo(SecurityContext.class); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void getValueWhenThreadLocalNotSetThenReturnsNull() { |
| 58 | + assertThat(this.threadLocalAccessor.getValue()).isNull(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void getValueWhenThreadLocalSetThenReturnsSecurityContextMono() { |
| 63 | + SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); |
| 64 | + securityContext.setAuthentication(new TestingAuthenticationToken("user", "password")); |
| 65 | + Mono<SecurityContext> mono = Mono.just(securityContext); |
| 66 | + this.threadLocalAccessor.setValue(mono); |
| 67 | + |
| 68 | + assertThat(this.threadLocalAccessor.getValue()).isSameAs(mono); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void getValueWhenThreadLocalSetOnAnotherThreadThenReturnsNull() throws InterruptedException { |
| 73 | + CountDownLatch threadLocalSet = new CountDownLatch(1); |
| 74 | + CountDownLatch threadLocalRead = new CountDownLatch(1); |
| 75 | + CountDownLatch threadLocalCleared = new CountDownLatch(1); |
| 76 | + |
| 77 | + Runnable task = () -> { |
| 78 | + SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); |
| 79 | + securityContext.setAuthentication(new TestingAuthenticationToken("user", "password")); |
| 80 | + Mono<SecurityContext> mono = Mono.just(securityContext); |
| 81 | + this.threadLocalAccessor.setValue(mono); |
| 82 | + threadLocalSet.countDown(); |
| 83 | + try { |
| 84 | + threadLocalRead.await(); |
| 85 | + } |
| 86 | + catch (InterruptedException ignored) { |
| 87 | + } |
| 88 | + finally { |
| 89 | + this.threadLocalAccessor.setValue(); |
| 90 | + threadLocalCleared.countDown(); |
| 91 | + } |
| 92 | + }; |
| 93 | + try (SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor()) { |
| 94 | + taskExecutor.execute(task); |
| 95 | + threadLocalSet.await(); |
| 96 | + assertThat(this.threadLocalAccessor.getValue()).isNull(); |
| 97 | + threadLocalRead.countDown(); |
| 98 | + threadLocalCleared.await(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void setValueWhenNullThenThrowsIllegalArgumentException() { |
| 104 | + // @formatter:off |
| 105 | + assertThatIllegalArgumentException() |
| 106 | + .isThrownBy(() -> this.threadLocalAccessor.setValue(null)) |
| 107 | + .withMessage("securityContext cannot be null"); |
| 108 | + // @formatter:on |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void setValueWhenThreadLocalSetThenClearsThreadLocal() { |
| 113 | + SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); |
| 114 | + securityContext.setAuthentication(new TestingAuthenticationToken("user", "password")); |
| 115 | + Mono<SecurityContext> mono = Mono.just(securityContext); |
| 116 | + this.threadLocalAccessor.setValue(mono); |
| 117 | + assertThat(this.threadLocalAccessor.getValue()).isSameAs(mono); |
| 118 | + |
| 119 | + this.threadLocalAccessor.setValue(); |
| 120 | + assertThat(this.threadLocalAccessor.getValue()).isNull(); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments