Skip to content

Commit 977db36

Browse files
committed
Make the loginProcessingUrl configurable for saml2Login()
Fixes spring-projectsgh-7565 spring-projects#7565
1 parent 5f17032 commit 977db36

File tree

6 files changed

+125
-7
lines changed

6 files changed

+125
-7
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ public void init(B http) throws Exception {
164164
this.relyingPartyRegistrationRepository = getSharedOrBean(http, RelyingPartyRegistrationRepository.class);
165165
}
166166

167-
Saml2WebSsoAuthenticationFilter webSsoFilter = new Saml2WebSsoAuthenticationFilter(this.relyingPartyRegistrationRepository);
167+
Saml2WebSsoAuthenticationFilter webSsoFilter = new Saml2WebSsoAuthenticationFilter(
168+
this.relyingPartyRegistrationRepository,
169+
this.loginProcessingUrl
170+
);
168171
setAuthenticationFilter(webSsoFilter);
169172
super.loginProcessingUrl(this.loginProcessingUrl);
170173

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/servlet/filter/Saml2WebSsoAuthenticationFilter.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,21 @@ public class Saml2WebSsoAuthenticationFilter extends AbstractAuthenticationProce
4444
private final RelyingPartyRegistrationRepository relyingPartyRegistrationRepository;
4545

4646
public Saml2WebSsoAuthenticationFilter(RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) {
47-
super(DEFAULT_FILTER_PROCESSES_URI);
47+
this(relyingPartyRegistrationRepository, DEFAULT_FILTER_PROCESSES_URI);
48+
}
49+
50+
public Saml2WebSsoAuthenticationFilter(
51+
RelyingPartyRegistrationRepository relyingPartyRegistrationRepository,
52+
String filterProcessesUrl) {
53+
super(filterProcessesUrl);
4854
Assert.notNull(relyingPartyRegistrationRepository, "relyingPartyRegistrationRepository cannot be null");
49-
this.matcher = new AntPathRequestMatcher(DEFAULT_FILTER_PROCESSES_URI);
55+
Assert.hasText(filterProcessesUrl, "filterProcessesUrl must contain a URL pattern");
56+
Assert.isTrue(
57+
filterProcessesUrl.contains("{registrationId}"),
58+
"filterProcessesUrl must contain a {registrationId} match variable"
59+
);
60+
this.matcher = new AntPathRequestMatcher(filterProcessesUrl);
61+
setRequiresAuthenticationRequestMatcher(this.matcher);
5062
this.relyingPartyRegistrationRepository = relyingPartyRegistrationRepository;
5163
setAllowSessionCreation(true);
5264
setSessionAuthenticationStrategy(new ChangeSessionIdAuthenticationStrategy());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.saml2.provider.service.servlet.filter;
18+
19+
import org.junit.Assert;
20+
import org.junit.Before;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.ExpectedException;
24+
import org.springframework.mock.web.MockHttpServletRequest;
25+
import org.springframework.mock.web.MockHttpServletResponse;
26+
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
27+
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
import static org.mockito.Mockito.mock;
31+
32+
public class Saml2WebSsoAuthenticationFilterTests {
33+
34+
private Saml2WebSsoAuthenticationFilter filter;
35+
private RelyingPartyRegistrationRepository repository = mock(RelyingPartyRegistrationRepository.class);
36+
private MockHttpServletRequest request = new MockHttpServletRequest();
37+
private HttpServletResponse response = new MockHttpServletResponse();
38+
39+
@Rule
40+
public ExpectedException exception = ExpectedException.none();
41+
42+
@Before
43+
public void setup() {
44+
filter = new Saml2WebSsoAuthenticationFilter(repository);
45+
request.setPathInfo("/login/saml2/sso/idp-registration-id");
46+
request.setParameter("SAMLResponse", "xml-data-goes-here");
47+
}
48+
49+
@Test
50+
public void constructingFilterWithMissingRegistrationIdVariableThenThrowsException() {
51+
exception.expect(IllegalArgumentException.class);
52+
exception.expectMessage("filterProcessesUrl must contain a {registrationId} match variable");
53+
filter = new Saml2WebSsoAuthenticationFilter(repository, "/url/missing/variable");
54+
}
55+
56+
@Test
57+
public void constructingFilterWithValidRegistrationIdVariableThenSucceeds() {
58+
filter = new Saml2WebSsoAuthenticationFilter(repository, "/url/variable/is/present/{registrationId}");
59+
}
60+
61+
@Test
62+
public void requiresAuthenticationWhenHappyPathThenReturnsTrue() {
63+
Assert.assertTrue(filter.requiresAuthentication(request, response));
64+
}
65+
66+
@Test
67+
public void requiresAuthenticationWhenCustomProcessingUrlThenReturnsTrue() {
68+
filter = new Saml2WebSsoAuthenticationFilter(repository, "/some/other/path/{registrationId}");
69+
request.setPathInfo("/some/other/path/idp-registration-id");
70+
request.setParameter("SAMLResponse", "xml-data-goes-here");
71+
Assert.assertTrue(filter.requiresAuthentication(request, response));
72+
}
73+
74+
75+
}

samples/boot/saml2login/src/integration-test/java/org/springframework/security/samples/Saml2LoginIntegrationTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5252
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
5353
import org.springframework.boot.test.context.SpringBootTest;
54-
import org.springframework.context.annotation.ComponentScan;
5554
import org.springframework.http.MediaType;
5655
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
5756
import org.springframework.test.context.junit4.SpringRunner;
@@ -111,7 +110,6 @@ public class Saml2LoginIntegrationTests {
111110

112111
@SpringBootConfiguration
113112
@EnableAutoConfiguration
114-
@ComponentScan(basePackages = "sample")
115113
public static class SpringBootApplicationTestConfig {
116114

117115
}

samples/javaconfig/saml2login/src/main/java/org/springframework/security/samples/config/SecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected void configure(HttpSecurity http) throws Exception {
7676
getSaml2AuthenticationConfiguration()
7777
)
7878
)
79+
.loginProcessingUrl("/sample/jc/saml2/sso/{registrationId}")
7980
;
8081
// @formatter:on
8182
}

samples/javaconfig/saml2login/src/test/java/org/springframework/security/samples/config/SecurityConfigTests.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,46 @@
1515
*/
1616
package org.springframework.security.samples.config;
1717

18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationFilter;
24+
import org.springframework.security.web.FilterChainProxy;
1825
import org.springframework.test.context.ContextConfiguration;
1926
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27+
import org.springframework.test.util.ReflectionTestUtils;
2028

21-
import org.junit.Test;
22-
import org.junit.runner.RunWith;
29+
import java.util.List;
30+
import javax.servlet.Filter;
2331

2432
@RunWith(SpringJUnit4ClassRunner.class)
2533
@ContextConfiguration(classes = SecurityConfig.class)
2634
public class SecurityConfigTests {
2735

36+
@Autowired
37+
ApplicationContext context;
38+
2839
@Test
2940
public void securityConfigurationLoads() {
3041
}
42+
43+
@Test
44+
public void filterWhenLoginProcessingUrlIsSetInJavaConfigThenTheFilterHasIt() {
45+
FilterChainProxy filterChain = context.getBean(FilterChainProxy.class);
46+
Assert.assertNotNull(filterChain);
47+
final List<Filter> filters = filterChain.getFilters("/sample/jc/saml2/sso/test-id");
48+
Assert.assertNotNull(filters);
49+
Saml2WebSsoAuthenticationFilter filter = (Saml2WebSsoAuthenticationFilter) filters
50+
.stream()
51+
.filter(
52+
f -> f instanceof Saml2WebSsoAuthenticationFilter
53+
)
54+
.findFirst()
55+
.get();
56+
final Object matcher = ReflectionTestUtils.getField(filter, "requiresAuthenticationRequestMatcher");
57+
final Object pattern = ReflectionTestUtils.getField(matcher, "pattern");
58+
Assert.assertEquals("loginProcessingUrl mismatch", "/sample/jc/saml2/sso/{registrationId}", pattern);
59+
}
3160
}

0 commit comments

Comments
 (0)