|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 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.
|
|
29 | 29 | import org.springframework.mock.web.MockHttpServletRequest;
|
30 | 30 | import org.springframework.mock.web.MockHttpServletResponse;
|
31 | 31 | import org.springframework.mock.web.MockHttpSession;
|
| 32 | +import org.springframework.security.MockSecurityContextHolderStrategy; |
| 33 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
32 | 34 | import org.springframework.security.core.context.SecurityContextHolder;
|
| 35 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
33 | 36 | import org.springframework.security.core.session.SessionInformation;
|
34 | 37 | import org.springframework.security.core.session.SessionRegistry;
|
35 | 38 | import org.springframework.security.core.session.SessionRegistryImpl;
|
|
47 | 50 | import static org.mockito.ArgumentMatchers.eq;
|
48 | 51 | import static org.mockito.BDDMockito.given;
|
49 | 52 | import static org.mockito.Mockito.mock;
|
| 53 | +import static org.mockito.Mockito.spy; |
50 | 54 | import static org.mockito.Mockito.verify;
|
51 | 55 | import static org.mockito.Mockito.verifyZeroInteractions;
|
52 | 56 |
|
@@ -267,6 +271,25 @@ public void doFilterWhenCustomLogoutHandlersThenHandlersUsed() throws Exception
|
267 | 271 | verify(handler).logout(eq(request), eq(response), any());
|
268 | 272 | }
|
269 | 273 |
|
| 274 | + @Test |
| 275 | + public void doFilterWhenCustomSecurityContextHolderStrategyThenHandlersUsed() throws Exception { |
| 276 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 277 | + MockHttpSession session = new MockHttpSession(); |
| 278 | + request.setSession(session); |
| 279 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 280 | + SessionRegistry registry = mock(SessionRegistry.class); |
| 281 | + SessionInformation information = new SessionInformation("user", "sessionId", |
| 282 | + new Date(System.currentTimeMillis() - 1000)); |
| 283 | + information.expireNow(); |
| 284 | + given(registry.getSessionInformation(anyString())).willReturn(information); |
| 285 | + ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry); |
| 286 | + SecurityContextHolderStrategy securityContextHolderStrategy = spy( |
| 287 | + new MockSecurityContextHolderStrategy(new TestingAuthenticationToken("user", "password"))); |
| 288 | + filter.setSecurityContextHolderStrategy(securityContextHolderStrategy); |
| 289 | + filter.doFilter(request, response, new MockFilterChain()); |
| 290 | + verify(securityContextHolderStrategy).getContext(); |
| 291 | + } |
| 292 | + |
270 | 293 | @Test
|
271 | 294 | public void setLogoutHandlersWhenNullThenThrowsException() {
|
272 | 295 | ConcurrentSessionFilter filter = new ConcurrentSessionFilter(new SessionRegistryImpl());
|
|
0 commit comments