|
45 | 45 | import org.springframework.security.authentication.AuthenticationManager;
|
46 | 46 | import org.springframework.security.authentication.AuthenticationServiceException;
|
47 | 47 | import org.springframework.security.config.Customizer;
|
| 48 | +import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; |
48 | 49 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
49 | 50 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
50 | 51 | import org.springframework.security.config.test.SpringTestContext;
|
|
53 | 54 | import org.springframework.security.core.AuthenticationException;
|
54 | 55 | import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
55 | 56 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
| 57 | +import org.springframework.security.core.context.SecurityContextChangedListener; |
| 58 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
56 | 59 | import org.springframework.security.saml2.core.Saml2ErrorCodes;
|
57 | 60 | import org.springframework.security.saml2.core.Saml2Utils;
|
58 | 61 | import org.springframework.security.saml2.core.TestSaml2X509Credentials;
|
|
91 | 94 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
92 | 95 | import static org.mockito.ArgumentMatchers.any;
|
93 | 96 | import static org.mockito.BDDMockito.given;
|
| 97 | +import static org.mockito.Mockito.atLeastOnce; |
94 | 98 | import static org.mockito.Mockito.mock;
|
95 | 99 | import static org.mockito.Mockito.spy;
|
| 100 | +import static org.mockito.Mockito.times; |
96 | 101 | import static org.mockito.Mockito.verify;
|
97 | 102 | import static org.springframework.security.config.Customizer.withDefaults;
|
| 103 | +import static org.springframework.security.config.annotation.SecurityContextChangedListenerArgumentMatchers.setAuthentication; |
98 | 104 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
99 | 105 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
100 | 106 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
@@ -162,6 +168,26 @@ public void saml2LoginWhenDefaultsThenSaml2AuthenticatedPrincipal() throws Excep
|
162 | 168 | // @formatter:on
|
163 | 169 | }
|
164 | 170 |
|
| 171 | + @Test |
| 172 | + public void saml2LoginWhenCustomSecurityContextHolderStrategyThenUses() throws Exception { |
| 173 | + this.spring |
| 174 | + .register(Saml2LoginConfig.class, SecurityContextChangedListenerConfig.class, ResourceController.class) |
| 175 | + .autowire(); |
| 176 | + // @formatter:off |
| 177 | + MockHttpSession session = (MockHttpSession) this.mvc |
| 178 | + .perform(post("/login/saml2/sso/registration-id") |
| 179 | + .param("SAMLResponse", SIGNED_RESPONSE)) |
| 180 | + .andExpect(redirectedUrl("/")).andReturn().getRequest().getSession(false); |
| 181 | + this.mvc.perform(get("/").session(session)) |
| 182 | + .andExpect(content().string("test@saml.user")); |
| 183 | + // @formatter:on |
| 184 | + SecurityContextHolderStrategy strategy = this.spring.getContext().getBean(SecurityContextHolderStrategy.class); |
| 185 | + verify(strategy, atLeastOnce()).getContext(); |
| 186 | + SecurityContextChangedListener listener = this.spring.getContext() |
| 187 | + .getBean(SecurityContextChangedListener.class); |
| 188 | + verify(listener, times(2)).securityContextChanged(setAuthentication(Saml2Authentication.class)); |
| 189 | + } |
| 190 | + |
165 | 191 | @Test
|
166 | 192 | public void saml2LoginWhenConfiguringAuthenticationManagerThenTheManagerIsUsed() throws Exception {
|
167 | 193 | // setup application context
|
|
0 commit comments