Skip to content

Commit c82528f

Browse files
committed
add SAML authentication request support to login configurer
Closes gh-8873
1 parent 4238f31 commit c82528f

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public final class Saml2LoginConfigurer<B extends HttpSecurityBuilder<B>>
109109

110110
private String loginPage;
111111

112-
private String authenticationRequestUri = "/saml2/authenticate/{registrationId}";
112+
private String authenticationRequestUri = Saml2AuthenticationRequestResolver.DEFAULT_AUTHENTICATION_REQUEST_URI;
113113

114114
private Saml2AuthenticationRequestResolver authenticationRequestResolver;
115115

@@ -186,6 +186,24 @@ public Saml2LoginConfigurer<B> authenticationRequestResolver(
186186
return this;
187187
}
188188

189+
/**
190+
* Customize the URL that the SAML Authentication Request will be sent to.
191+
* @param authenticationRequestUri the URI to use for the SAML 2.0 Authentication
192+
* Request
193+
* @return the {@link Saml2LoginConfigurer} for further configuration
194+
* @since 6.0
195+
*/
196+
public Saml2LoginConfigurer<B> authenticationRequestUri(String authenticationRequestUri) {
197+
// OpenSAML 3 is no longer supported by spring security
198+
if (version().startsWith("3")) {
199+
return this;
200+
}
201+
Assert.state(authenticationRequestUri.contains("{registrationId}"),
202+
"authenticationRequestUri must contain {registrationId} path variable");
203+
this.authenticationRequestUri = authenticationRequestUri;
204+
return this;
205+
}
206+
189207
/**
190208
* Specifies the URL to validate the credentials. If specified a custom URL, consider
191209
* specifying a custom {@link AuthenticationConverter} via
@@ -307,7 +325,11 @@ private Saml2AuthenticationRequestResolver getAuthenticationRequestResolver(B ht
307325
return bean;
308326
}
309327
if (version().startsWith("4")) {
310-
return new OpenSaml4AuthenticationRequestResolver(relyingPartyRegistrationResolver(http));
328+
OpenSaml4AuthenticationRequestResolver openSaml4AuthenticationRequestResolver = new OpenSaml4AuthenticationRequestResolver(
329+
relyingPartyRegistrationResolver(http));
330+
openSaml4AuthenticationRequestResolver
331+
.setRequestMatcher(new AntPathRequestMatcher(this.authenticationRequestUri));
332+
return openSaml4AuthenticationRequestResolver;
311333
}
312334
return new OpenSaml3AuthenticationRequestResolver(relyingPartyRegistrationResolver(http));
313335
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ public void authenticateWhenCustomAuthnRequestRepositoryThenUses() throws Except
297297
verify(repository).removeAuthenticationRequest(any(HttpServletRequest.class), any(HttpServletResponse.class));
298298
}
299299

300+
@Test
301+
public void authenticationRequestWhenCustomAuthenticationRequestUriRepositoryThenUses() throws Exception {
302+
this.spring.register(CustomAuthenticationRequestUriCustomAuthenticationConverter.class).autowire();
303+
MockHttpServletRequestBuilder request = get("/custom/auth/registration-id");
304+
this.mvc.perform(request).andExpect(status().isFound());
305+
Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> repository = this.spring.getContext()
306+
.getBean(Saml2AuthenticationRequestRepository.class);
307+
verify(repository).saveAuthenticationRequest(any(AbstractSaml2AuthenticationRequest.class),
308+
any(HttpServletRequest.class), any(HttpServletResponse.class));
309+
}
310+
300311
@Test
301312
public void saml2LoginWhenLoginProcessingUrlWithoutRegistrationIdAndDefaultAuthenticationConverterThenValidates() {
302313
assertThatExceptionOfType(BeanCreationException.class)
@@ -601,6 +612,30 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
601612

602613
}
603614

615+
@EnableWebSecurity
616+
@Import(Saml2LoginConfigBeans.class)
617+
static class CustomAuthenticationRequestUriCustomAuthenticationConverter {
618+
619+
private final Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> repository = mock(
620+
Saml2AuthenticationRequestRepository.class);
621+
622+
@Bean
623+
Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> authenticationRequestRepository() {
624+
return this.repository;
625+
}
626+
627+
@Bean
628+
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
629+
// @formatter:off
630+
http
631+
.authorizeRequests((authz) -> authz.anyRequest().authenticated())
632+
.saml2Login((saml2) -> saml2.authenticationRequestUri("/custom/auth/{registrationId}"));
633+
// @formatter:on
634+
return http.build();
635+
}
636+
637+
}
638+
604639
@EnableWebSecurity
605640
@Import(Saml2LoginConfigBeans.class)
606641
static class CustomLoginProcessingUrlCustomAuthenticationConverter {

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/authentication/OpenSamlAuthenticationRequestResolver.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ class OpenSamlAuthenticationRequestResolver {
5959
static {
6060
OpenSamlInitializationService.initialize();
6161
}
62-
63-
private final RequestMatcher requestMatcher = new AntPathRequestMatcher("/saml2/authenticate/{registrationId}");
64-
6562
private final RelyingPartyRegistrationResolver relyingPartyRegistrationResolver;
6663

6764
private final AuthnRequestBuilder authnRequestBuilder;
@@ -72,6 +69,9 @@ class OpenSamlAuthenticationRequestResolver {
7269

7370
private final NameIDBuilder nameIdBuilder;
7471

72+
private RequestMatcher requestMatcher = new AntPathRequestMatcher(
73+
Saml2AuthenticationRequestResolver.DEFAULT_AUTHENTICATION_REQUEST_URI);
74+
7575
private Converter<HttpServletRequest, String> relayStateResolver = (request) -> UUID.randomUUID().toString();
7676

7777
/**

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/authentication/Saml2AuthenticationRequestResolver.java

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030
public interface Saml2AuthenticationRequestResolver {
3131

32+
String DEFAULT_AUTHENTICATION_REQUEST_URI = "/saml2/authenticate/{registrationId}";
33+
3234
<T extends AbstractSaml2AuthenticationRequest> T resolve(HttpServletRequest request);
3335

3436
}

0 commit comments

Comments
 (0)