Skip to content

Commit c2d79fc

Browse files
committed
Add Conditions to Generating AuthnRequest
Closes gh-11657
1 parent aa22594 commit c2d79fc

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,16 @@
4747
import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationRequestRepository;
4848
import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationTokenConverter;
4949
import org.springframework.security.saml2.provider.service.web.authentication.Saml2AuthenticationRequestResolver;
50+
import org.springframework.security.web.AuthenticationEntryPoint;
5051
import org.springframework.security.web.authentication.AuthenticationConverter;
52+
import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint;
5153
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
5254
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
55+
import org.springframework.security.web.util.matcher.AndRequestMatcher;
5356
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
57+
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
58+
import org.springframework.security.web.util.matcher.OrRequestMatcher;
59+
import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher;
5460
import org.springframework.security.web.util.matcher.RequestMatcher;
5561
import org.springframework.util.Assert;
5662
import org.springframework.util.ClassUtils;
@@ -252,8 +258,7 @@ public void init(B http) throws Exception {
252258
this.updateAuthenticationDefaults();
253259
this.updateAccessDefaults(http);
254260
String loginUrl = providerUrlMap.entrySet().iterator().next().getKey();
255-
final LoginUrlAuthenticationEntryPoint entryPoint = new LoginUrlAuthenticationEntryPoint(loginUrl);
256-
registerAuthenticationEntryPoint(http, entryPoint);
261+
registerAuthenticationEntryPoint(http, getLoginEntryPoint(http, loginUrl));
257262
}
258263
else {
259264
super.init(http);
@@ -295,6 +300,22 @@ RelyingPartyRegistrationRepository relyingPartyRegistrationRepository(B http) {
295300
return this.relyingPartyRegistrationRepository;
296301
}
297302

303+
private AuthenticationEntryPoint getLoginEntryPoint(B http, String providerLoginPage) {
304+
RequestMatcher loginPageMatcher = new AntPathRequestMatcher(this.getLoginPage());
305+
RequestMatcher faviconMatcher = new AntPathRequestMatcher("/favicon.ico");
306+
RequestMatcher defaultEntryPointMatcher = this.getAuthenticationEntryPointMatcher(http);
307+
RequestMatcher defaultLoginPageMatcher = new AndRequestMatcher(
308+
new OrRequestMatcher(loginPageMatcher, faviconMatcher), defaultEntryPointMatcher);
309+
RequestMatcher notXRequestedWith = new NegatedRequestMatcher(
310+
new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"));
311+
LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints = new LinkedHashMap<>();
312+
entryPoints.put(new AndRequestMatcher(notXRequestedWith, new NegatedRequestMatcher(defaultLoginPageMatcher)),
313+
new LoginUrlAuthenticationEntryPoint(providerLoginPage));
314+
DelegatingAuthenticationEntryPoint loginEntryPoint = new DelegatingAuthenticationEntryPoint(entryPoints);
315+
loginEntryPoint.setDefaultEntryPoint(this.getAuthenticationEntryPoint());
316+
return loginEntryPoint;
317+
}
318+
298319
private void setAuthenticationRequestRepository(B http,
299320
Saml2WebSsoAuthenticationFilter saml2WebSsoAuthenticationFilter) {
300321
saml2WebSsoAuthenticationFilter.setAuthenticationRequestRepository(getAuthenticationRequestRepository(http));

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.context.annotation.Bean;
4444
import org.springframework.context.annotation.Import;
4545
import org.springframework.core.convert.converter.Converter;
46+
import org.springframework.http.MediaType;
4647
import org.springframework.mock.web.MockFilterChain;
4748
import org.springframework.mock.web.MockHttpServletRequest;
4849
import org.springframework.mock.web.MockHttpServletResponse;
@@ -412,6 +413,16 @@ public void authenticateWhenCustomLoginProcessingUrlAndSaml2AuthenticationTokenC
412413
verify(authenticationConverter).convert(any(HttpServletRequest.class));
413414
}
414415

416+
// gh-11657
417+
@Test
418+
public void getFaviconWhenDefaultConfigurationThenDoesNotSaveAuthnRequest() throws Exception {
419+
this.spring.register(Saml2LoginConfig.class).autowire();
420+
this.mvc.perform(get("/favicon.ico").accept(MediaType.TEXT_HTML)).andExpect(status().isFound())
421+
.andExpect(redirectedUrl("http://localhost/login"));
422+
this.mvc.perform(get("/").accept(MediaType.TEXT_HTML)).andExpect(status().isFound())
423+
.andExpect(redirectedUrl("http://localhost/saml2/authenticate/registration-id"));
424+
}
425+
415426
private void validateSaml2WebSsoAuthenticationFilterConfiguration() {
416427
// get the OpenSamlAuthenticationProvider
417428
Saml2WebSsoAuthenticationFilter filter = getSaml2SsoFilter(this.springSecurityFilterChain);

0 commit comments

Comments
 (0)