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