Skip to content

Commit ee66850

Browse files
committed
Add SecurityContextHolderStrategy for Jaas
Issue gh-11060 Issue gh-11061
1 parent 52d8e10 commit ee66850

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

config/src/main/java/org/springframework/security/config/http/HttpConfigurationBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ private void createJaasApiFilter() {
611611
provideJaasApi = DEF_JAAS_API_PROVISION;
612612
}
613613
if ("true".equals(provideJaasApi)) {
614-
this.jaasApiFilter = new RootBeanDefinition(JaasApiIntegrationFilter.class);
614+
this.jaasApiFilter = BeanDefinitionBuilder.rootBeanDefinition(JaasApiIntegrationFilter.class)
615+
.addPropertyValue("securityContextHolderStrategy", this.holderStrategyRef).getBeanDefinition();
615616
}
616617
}
617618

core/src/main/java/org/springframework/security/authentication/jaas/SecurityContextLoginModule.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import org.springframework.security.core.Authentication;
3030
import org.springframework.security.core.context.SecurityContextHolder;
31+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
32+
import org.springframework.util.Assert;
3133

3234
/**
3335
* An implementation of {@link LoginModule} that uses a Spring Security
@@ -55,6 +57,9 @@ public class SecurityContextLoginModule implements LoginModule {
5557

5658
private static final Log log = LogFactory.getLog(SecurityContextLoginModule.class);
5759

60+
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
61+
.getContextHolderStrategy();
62+
5863
private Authentication authen;
5964

6065
private Subject subject;
@@ -93,6 +98,17 @@ public boolean commit() {
9398
return true;
9499
}
95100

101+
/**
102+
* Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
103+
* the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
104+
*
105+
* @since 5.8
106+
*/
107+
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
108+
Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
109+
this.securityContextHolderStrategy = securityContextHolderStrategy;
110+
}
111+
96112
Authentication getAuthentication() {
97113
return this.authen;
98114
}
@@ -129,7 +145,7 @@ public void initialize(Subject subject, CallbackHandler callbackHandler, Map sha
129145
*/
130146
@Override
131147
public boolean login() throws LoginException {
132-
this.authen = SecurityContextHolder.getContext().getAuthentication();
148+
this.authen = this.securityContextHolderStrategy.getContext().getAuthentication();
133149
if (this.authen != null) {
134150
return true;
135151
}

core/src/test/java/org/springframework/security/authentication/jaas/SecurityContextLoginModuleTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929

3030
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
3131
import org.springframework.security.core.context.SecurityContextHolder;
32+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
33+
import org.springframework.security.core.context.SecurityContextImpl;
3234

3335
import static org.assertj.core.api.Assertions.assertThat;
3436
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
37+
import static org.mockito.BDDMockito.given;
38+
import static org.mockito.Mockito.mock;
3539

3640
/**
3741
* Tests SecurityContextLoginModule
@@ -84,6 +88,18 @@ public void testLoginSuccess() throws Exception {
8488
.withFailMessage("Principals should contain the authentication").isTrue();
8589
}
8690

91+
@Test
92+
public void loginWhenCustomSecurityContextHolderStrategyThenUses() throws Exception {
93+
SecurityContextHolderStrategy securityContextHolderStrategy = mock(SecurityContextHolderStrategy.class);
94+
given(securityContextHolderStrategy.getContext()).willReturn(new SecurityContextImpl(this.auth));
95+
this.module.setSecurityContextHolderStrategy(securityContextHolderStrategy);
96+
assertThat(this.module.login()).as("Login should succeed, there is an authentication set").isTrue();
97+
assertThat(this.module.commit()).withFailMessage("The authentication is not null, this should return true")
98+
.isTrue();
99+
assertThat(this.subject.getPrincipals().contains(this.auth))
100+
.withFailMessage("Principals should contain the authentication").isTrue();
101+
}
102+
87103
@Test
88104
public void testLogout() throws Exception {
89105
SecurityContextHolder.getContext().setAuthentication(this.auth);

web/src/main/java/org/springframework/security/web/jaasapi/JaasApiIntegrationFilter.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2021 the original author or authors.
2+
* Copyright 2010-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.
@@ -31,6 +31,8 @@
3131
import org.springframework.security.authentication.jaas.JaasAuthenticationToken;
3232
import org.springframework.security.core.Authentication;
3333
import org.springframework.security.core.context.SecurityContextHolder;
34+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
35+
import org.springframework.util.Assert;
3436
import org.springframework.web.filter.GenericFilterBean;
3537

3638
/**
@@ -51,6 +53,9 @@
5153
*/
5254
public class JaasApiIntegrationFilter extends GenericFilterBean {
5355

56+
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
57+
.getContextHolderStrategy();
58+
5459
private boolean createEmptySubject;
5560

5661
/**
@@ -113,7 +118,7 @@ public final void doFilter(ServletRequest request, ServletResponse response, Fil
113118
* available.
114119
*/
115120
protected Subject obtainSubject(ServletRequest request) {
116-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
121+
Authentication authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
117122
this.logger.debug(LogMessage.format("Attempting to obtainSubject using authentication : %s", authentication));
118123
if (authentication == null) {
119124
return null;
@@ -143,4 +148,15 @@ public final void setCreateEmptySubject(boolean createEmptySubject) {
143148
this.createEmptySubject = createEmptySubject;
144149
}
145150

151+
/**
152+
* Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
153+
* the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
154+
*
155+
* @since 5.8
156+
*/
157+
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
158+
Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
159+
this.securityContextHolderStrategy = securityContextHolderStrategy;
160+
}
161+
146162
}

web/src/test/java/org/springframework/security/web/jaasapi/JaasApiIntegrationFilterTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@
4848
import org.springframework.security.core.Authentication;
4949
import org.springframework.security.core.authority.AuthorityUtils;
5050
import org.springframework.security.core.context.SecurityContextHolder;
51+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
52+
import org.springframework.security.core.context.SecurityContextImpl;
5153

5254
import static org.assertj.core.api.Assertions.assertThat;
55+
import static org.mockito.BDDMockito.given;
56+
import static org.mockito.Mockito.mock;
5357

5458
/**
5559
* Tests the JaasApiIntegrationFilter.
@@ -189,6 +193,14 @@ public void doFilterAuthenticationNull() throws Exception {
189193
assertJaasSubjectEquals(new Subject());
190194
}
191195

196+
@Test
197+
public void doFilterUsesCustomSecurityContextHolderStrategy() throws Exception {
198+
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
199+
given(strategy.getContext()).willReturn(new SecurityContextImpl(this.token));
200+
this.filter.setSecurityContextHolderStrategy(strategy);
201+
assertJaasSubjectEquals(this.authenticatedSubject);
202+
}
203+
192204
private void assertJaasSubjectEquals(final Subject expectedValue) throws Exception {
193205
MockFilterChain chain = new MockFilterChain() {
194206
@Override

0 commit comments

Comments
 (0)