Skip to content

Commit fa0086d

Browse files
committed
Polish SecurityContextHolderStrategy Java Configuration for Defaults
Issue gh-11061
1 parent 772f29e commit fa0086d

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configuration/WebMvcSecurityConfiguration.java

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
6666
.add(new org.springframework.security.web.bind.support.AuthenticationPrincipalArgumentResolver());
6767
CurrentSecurityContextArgumentResolver currentSecurityContextArgumentResolver = new CurrentSecurityContextArgumentResolver();
6868
currentSecurityContextArgumentResolver.setBeanResolver(this.beanResolver);
69+
currentSecurityContextArgumentResolver.setSecurityContextHolderStrategy(this.securityContextHolderStrategy);
6970
argumentResolvers.add(currentSecurityContextArgumentResolver);
7071
argumentResolvers.add(new CsrfTokenArgumentResolver());
7172
}

config/src/main/java/org/springframework/security/config/annotation/web/configurers/AbstractInterceptUrlConfigurer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -144,6 +144,7 @@ private FilterSecurityInterceptor createFilterSecurityInterceptor(H http,
144144
securityInterceptor.setSecurityMetadataSource(metadataSource);
145145
securityInterceptor.setAccessDecisionManager(getAccessDecisionManager(http));
146146
securityInterceptor.setAuthenticationManager(authenticationManager);
147+
securityInterceptor.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy());
147148
securityInterceptor.afterPropertiesSet();
148149
return securityInterceptor;
149150
}

config/src/main/java/org/springframework/security/config/annotation/web/configurers/AnonymousConfigurer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -148,6 +148,7 @@ public void init(H http) {
148148
this.authenticationFilter = new AnonymousAuthenticationFilter(getKey(), this.principal, this.authorities);
149149
this.authenticationFilter.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy());
150150
}
151+
this.authenticationFilter.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy());
151152
this.authenticationProvider = postProcess(this.authenticationProvider);
152153
http.authenticationProvider(this.authenticationProvider);
153154
}

config/src/main/java/org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -325,12 +325,14 @@ public List<LogoutHandler> getLogoutHandlers() {
325325
* @return the {@link LogoutFilter} to use.
326326
*/
327327
private LogoutFilter createLogoutFilter(H http) {
328+
this.contextLogoutHandler.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy());
328329
this.logoutHandlers.add(this.contextLogoutHandler);
329330
this.logoutHandlers.add(postProcess(new LogoutSuccessEventPublishingLogoutHandler()));
330331
LogoutHandler[] handlers = this.logoutHandlers.toArray(new LogoutHandler[0]);
331332
LogoutFilter result = new LogoutFilter(getLogoutSuccessHandler(), handlers);
332333
result.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy());
333334
result.setLogoutRequestMatcher(getLogoutRequestMatcher(http));
335+
result.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy());
334336
result = postProcess(result);
335337
return result;
336338
}

config/src/main/java/org/springframework/security/config/annotation/web/configurers/ServletApiConfigurer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -98,6 +98,7 @@ public void configure(H http) {
9898
.getBean(grantedAuthorityDefaultsBeanNames[0], GrantedAuthorityDefaults.class);
9999
this.securityContextRequestFilter.setRolePrefix(grantedAuthorityDefaults.getRolePrefix());
100100
}
101+
this.securityContextRequestFilter.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy());
101102
}
102103
this.securityContextRequestFilter = postProcess(this.securityContextRequestFilter);
103104
http.addFilter(this.securityContextRequestFilter);

config/src/test/java/org/springframework/security/config/annotation/web/configurers/LogoutConfigurerTests.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -25,12 +25,14 @@
2525
import org.springframework.context.annotation.Bean;
2626
import org.springframework.http.MediaType;
2727
import org.springframework.security.config.annotation.ObjectPostProcessor;
28+
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
2829
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2930
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3031
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3132
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
3233
import org.springframework.security.config.test.SpringTestContext;
3334
import org.springframework.security.config.test.SpringTestContextExtension;
35+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
3436
import org.springframework.security.web.authentication.RememberMeServices;
3537
import org.springframework.security.web.authentication.logout.LogoutFilter;
3638
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
@@ -40,6 +42,7 @@
4042

4143
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4244
import static org.mockito.ArgumentMatchers.any;
45+
import static org.mockito.Mockito.atLeastOnce;
4346
import static org.mockito.Mockito.mock;
4447
import static org.mockito.Mockito.spy;
4548
import static org.mockito.Mockito.verify;
@@ -241,6 +244,22 @@ public void logoutWhenAcceptTextHtmlThenRedirectsToLogin() throws Exception {
241244
// @formatter:on
242245
}
243246

247+
@Test
248+
public void logoutWhenCustomSecurityContextHolderStrategyThenUses() throws Exception {
249+
this.spring.register(BasicSecurityConfig.class, SecurityContextChangedListenerConfig.class).autowire();
250+
// @formatter:off
251+
MockHttpServletRequestBuilder logoutRequest = post("/logout")
252+
.with(csrf())
253+
.with(user("user"))
254+
.header(HttpHeaders.ACCEPT, MediaType.TEXT_HTML_VALUE);
255+
this.mvc.perform(logoutRequest)
256+
.andExpect(status().isFound())
257+
.andExpect(redirectedUrl("/login?logout"));
258+
// @formatter:on
259+
SecurityContextHolderStrategy strategy = this.spring.getContext().getBean(SecurityContextHolderStrategy.class);
260+
verify(strategy, atLeastOnce()).getContext();
261+
}
262+
244263
// gh-3282
245264
@Test
246265
public void logoutWhenAcceptApplicationJsonThenReturnsStatusNoContent() throws Exception {

0 commit comments

Comments
 (0)