This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/spring-session in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git
commit d907f315b3231ee5399fb6ce7641902316482816 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Sat Dec 5 15:41:29 2026 +0000 Change the session id on federated login in the Spring plugin --- plugins/spring/pom.xml | 5 ++ .../spring/web/FederationAuthenticationFilter.java | 4 ++ .../web/FederationAuthenticationFilterTest.java | 75 ++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/plugins/spring/pom.xml b/plugins/spring/pom.xml index 82604438..c6d0a2b9 100644 --- a/plugins/spring/pom.xml +++ b/plugins/spring/pom.xml @@ -48,6 +48,11 @@ <version>${junit.version}</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>org.apache.cxf.fediz</groupId> <artifactId>fediz-core</artifactId> diff --git a/plugins/spring/src/main/java/org/apache/cxf/fediz/spring/web/FederationAuthenticationFilter.java b/plugins/spring/src/main/java/org/apache/cxf/fediz/spring/web/FederationAuthenticationFilter.java index b5f2fa15..58fcbec4 100644 --- a/plugins/spring/src/main/java/org/apache/cxf/fediz/spring/web/FederationAuthenticationFilter.java +++ b/plugins/spring/src/main/java/org/apache/cxf/fediz/spring/web/FederationAuthenticationFilter.java @@ -42,6 +42,7 @@ import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; +import org.springframework.security.web.authentication.session.ChangeSessionIdAuthenticationStrategy; import static org.apache.cxf.fediz.spring.web.FederationAuthenticationEntryPoint.SAVED_CONTEXT; @@ -53,6 +54,9 @@ public class FederationAuthenticationFilter extends AbstractAuthenticationProces public FederationAuthenticationFilter() { super("/j_spring_fediz_security_check"); setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler()); + // Change the session id on successful login to prevent session fixation. This can be + // overridden via setSessionAuthenticationStrategy. + setSessionAuthenticationStrategy(new ChangeSessionIdAuthenticationStrategy()); } @Override diff --git a/plugins/spring/src/test/java/org/apache/cxf/fediz/spring/web/FederationAuthenticationFilterTest.java b/plugins/spring/src/test/java/org/apache/cxf/fediz/spring/web/FederationAuthenticationFilterTest.java new file mode 100644 index 00000000..6f6341ea --- /dev/null +++ b/plugins/spring/src/test/java/org/apache/cxf/fediz/spring/web/FederationAuthenticationFilterTest.java @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.fediz.spring.web; + +import java.util.Collections; + +import org.apache.cxf.fediz.core.RequestState; +import org.springframework.mock.web.MockFilterChain; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.context.SecurityContextHolder; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.apache.cxf.fediz.spring.web.FederationAuthenticationEntryPoint.SAVED_CONTEXT; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class FederationAuthenticationFilterTest { + + @AfterEach + public void clearContext() { + SecurityContextHolder.clearContext(); + } + + @Test + public void testSessionIdChangedOnSuccessfulLogin() throws Exception { + FederationAuthenticationFilter filter = new FederationAuthenticationFilter(); + filter.setAuthenticationManager(authentication -> + new UsernamePasswordAuthenticationToken("alice", null, + Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")))); + filter.afterPropertiesSet(); + + String state = "state-123"; + MockHttpSession session = new MockHttpSession(); + session.setAttribute(SAVED_CONTEXT, new RequestState(null, null, null, null, null, null, state, 0L)); + session.setAttribute("appAttribute", "value"); + String originalSessionId = session.getId(); + + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/j_spring_fediz_security_check"); + request.setServletPath("/j_spring_fediz_security_check"); + request.setSession(session); + request.setParameter("wa", "wsignin1.0"); + request.setParameter("wresult", "<token/>"); + request.setParameter("wctx", state); + + filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain()); + + assertNotNull(SecurityContextHolder.getContext().getAuthentication()); + assertNotEquals(originalSessionId, request.getSession().getId()); + // Session attributes are kept + assertEquals("value", request.getSession().getAttribute("appAttribute")); + } +}
