|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.cas.web;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | + |
19 | 21 | import jakarta.servlet.FilterChain;
|
| 22 | +import jakarta.servlet.ServletException; |
20 | 23 | import jakarta.servlet.http.HttpSession;
|
21 | 24 | import org.apereo.cas.client.proxy.ProxyGrantingTicketStorage;
|
22 | 25 | import org.junit.jupiter.api.AfterEach;
|
|
35 | 38 | import org.springframework.security.core.authority.AuthorityUtils;
|
36 | 39 | import org.springframework.security.core.context.SecurityContext;
|
37 | 40 | import org.springframework.security.core.context.SecurityContextHolder;
|
| 41 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
| 42 | +import org.springframework.security.core.context.SecurityContextImpl; |
38 | 43 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
39 | 44 | import org.springframework.security.web.context.SecurityContextRepository;
|
40 | 45 | import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
@@ -240,4 +245,25 @@ public void attemptAuthenticationWhenNoServiceTicketAndIsGatewayRequestThenRedir
|
240 | 245 | .isNull();
|
241 | 246 | }
|
242 | 247 |
|
| 248 | + @Test |
| 249 | + void successfulAuthenticationWhenSecurityContextRepositorySetThenUses() throws ServletException, IOException { |
| 250 | + SecurityContextRepository securityContextRepository = mock(SecurityContextRepository.class); |
| 251 | + CasAuthenticationFilter filter = new CasAuthenticationFilter(); |
| 252 | + filter.setSecurityContextRepository(securityContextRepository); |
| 253 | + filter.successfulAuthentication(new MockHttpServletRequest(), new MockHttpServletResponse(), |
| 254 | + new MockFilterChain(), mock(Authentication.class)); |
| 255 | + verify(securityContextRepository).saveContext(any(SecurityContext.class), any(), any()); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + void successfulAuthenticationWhenSecurityContextHolderStrategySetThenUses() throws ServletException, IOException { |
| 260 | + SecurityContextHolderStrategy securityContextRepository = mock(SecurityContextHolderStrategy.class); |
| 261 | + given(securityContextRepository.createEmptyContext()).willReturn(new SecurityContextImpl()); |
| 262 | + CasAuthenticationFilter filter = new CasAuthenticationFilter(); |
| 263 | + filter.setSecurityContextHolderStrategy(securityContextRepository); |
| 264 | + filter.successfulAuthentication(new MockHttpServletRequest(), new MockHttpServletResponse(), |
| 265 | + new MockFilterChain(), mock(Authentication.class)); |
| 266 | + verify(securityContextRepository).setContext(any(SecurityContext.class)); |
| 267 | + } |
| 268 | + |
243 | 269 | }
|
0 commit comments