Skip to content

Commit fd3de41

Browse files
Merge branch '6.2.x'
Closes spring-projectsgh-14537
2 parents 29f20de + b21f941 commit fd3de41

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

cas/src/main/java/org/springframework/security/cas/web/CasAuthenticationFilter.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -329,6 +329,18 @@ public final void setServiceProperties(final ServiceProperties serviceProperties
329329
this.authenticateAllArtifacts = serviceProperties.isAuthenticateAllArtifacts();
330330
}
331331

332+
@Override
333+
public void setSecurityContextRepository(SecurityContextRepository securityContextRepository) {
334+
super.setSecurityContextRepository(securityContextRepository);
335+
this.securityContextRepository = securityContextRepository;
336+
}
337+
338+
@Override
339+
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
340+
super.setSecurityContextHolderStrategy(securityContextHolderStrategy);
341+
this.securityContextHolderStrategy = securityContextHolderStrategy;
342+
}
343+
332344
/**
333345
* Set the {@link RedirectStrategy} used to redirect to the saved request if there is
334346
* one saved. Defaults to {@link DefaultRedirectStrategy}.

cas/src/test/java/org/springframework/security/cas/web/CasAuthenticationFilterTests.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
1616

1717
package org.springframework.security.cas.web;
1818

19+
import java.io.IOException;
20+
1921
import jakarta.servlet.FilterChain;
22+
import jakarta.servlet.ServletException;
2023
import jakarta.servlet.http.HttpSession;
2124
import org.apereo.cas.client.proxy.ProxyGrantingTicketStorage;
2225
import org.junit.jupiter.api.AfterEach;
@@ -35,6 +38,8 @@
3538
import org.springframework.security.core.authority.AuthorityUtils;
3639
import org.springframework.security.core.context.SecurityContext;
3740
import org.springframework.security.core.context.SecurityContextHolder;
41+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
42+
import org.springframework.security.core.context.SecurityContextImpl;
3843
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
3944
import org.springframework.security.web.context.SecurityContextRepository;
4045
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
@@ -240,4 +245,25 @@ public void attemptAuthenticationWhenNoServiceTicketAndIsGatewayRequestThenRedir
240245
.isNull();
241246
}
242247

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+
243269
}

0 commit comments

Comments
 (0)