Skip to content

Commit a2d60f6

Browse files
eleftheriaskostya05983
authored andcommitted
Allow configuration of requires channel through nested builder
Issue: spring-projectsgh-5557
1 parent ec09b62 commit a2d60f6

File tree

3 files changed

+81
-9
lines changed

3 files changed

+81
-9
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

+50-3
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,10 @@ public PortMapperConfigurer<HttpSecurity> portMapper() throws Exception {
658658
* &#064;Override
659659
* protected void configure(HttpSecurity http) throws Exception {
660660
* http
661-
* .requiresChannel()
662-
* .anyRequest().requiresSecure()
663-
* .and()
661+
* .requiresChannel(requiresChannel ->
662+
* requiresChannel
663+
* .anyRequest().requiresSecure()
664+
* )
664665
* .portMapper(portMapper ->
665666
* portMapper
666667
* .http(9090).mapsTo(9443)
@@ -1894,6 +1895,52 @@ public ChannelSecurityConfigurer<HttpSecurity>.ChannelRequestMatcherRegistry req
18941895
.getRegistry();
18951896
}
18961897

1898+
/**
1899+
* Configures channel security. In order for this configuration to be useful at least
1900+
* one mapping to a required channel must be provided.
1901+
*
1902+
* <h2>Example Configuration</h2>
1903+
*
1904+
* The example below demonstrates how to require HTTPs for every request. Only
1905+
* requiring HTTPS for some requests is supported, but not recommended since an
1906+
* application that allows for HTTP introduces many security vulnerabilities. For one
1907+
* such example, read about <a
1908+
* href="https://en.wikipedia.org/wiki/Firesheep">Firesheep</a>.
1909+
*
1910+
* <pre>
1911+
* &#064;Configuration
1912+
* &#064;EnableWebSecurity
1913+
* public class ChannelSecurityConfig extends WebSecurityConfigurerAdapter {
1914+
*
1915+
* &#064;Override
1916+
* protected void configure(HttpSecurity http) throws Exception {
1917+
* http
1918+
* .authorizeRequests(authorizeRequests ->
1919+
* authorizeRequests
1920+
* .antMatchers(&quot;/**&quot;).hasRole(&quot;USER&quot;)
1921+
* )
1922+
* .formLogin(withDefaults())
1923+
* .requiresChannel(requiresChannel ->
1924+
* requiresChannel
1925+
* .anyRequest().requiresSecure()
1926+
* );
1927+
* }
1928+
* }
1929+
* </pre>
1930+
*
1931+
* @param requiresChannelCustomizer the {@link Customizer} to provide more options for
1932+
* the {@link ChannelSecurityConfigurer.ChannelRequestMatcherRegistry}
1933+
* @return the {@link HttpSecurity} for further customizations
1934+
* @throws Exception
1935+
*/
1936+
public HttpSecurity requiresChannel(Customizer<ChannelSecurityConfigurer<HttpSecurity>.ChannelRequestMatcherRegistry> requiresChannelCustomizer)
1937+
throws Exception {
1938+
ApplicationContext context = getContext();
1939+
requiresChannelCustomizer.customize(getOrApply(new ChannelSecurityConfigurer<>(context))
1940+
.getRegistry());
1941+
return HttpSecurity.this;
1942+
}
1943+
18971944
/**
18981945
* Configures HTTP Basic authentication.
18991946
*

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

+23
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,27 @@ protected void configure(HttpSecurity http) throws Exception {
135135
// @formatter:on
136136
}
137137
}
138+
139+
@Test
140+
public void requestWhenRequiresChannelConfiguredInLambdaThenRedirectsToHttps() throws Exception {
141+
this.spring.register(RequiresChannelInLambdaConfig.class).autowire();
142+
143+
mvc.perform(get("/"))
144+
.andExpect(redirectedUrl("https://localhost/"));
145+
}
146+
147+
@EnableWebSecurity
148+
static class RequiresChannelInLambdaConfig extends WebSecurityConfigurerAdapter {
149+
150+
@Override
151+
protected void configure(HttpSecurity http) throws Exception {
152+
// @formatter:off
153+
http
154+
.requiresChannel(requiresChannel ->
155+
requiresChannel
156+
.anyRequest().requiresSecure()
157+
);
158+
// @formatter:on
159+
}
160+
}
138161
}

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -79,9 +79,10 @@ static class HttpMapsToInLambdaConfig extends WebSecurityConfigurerAdapter {
7979
protected void configure(HttpSecurity http) throws Exception {
8080
// @formatter:off
8181
http
82-
.requiresChannel()
82+
.requiresChannel(requiresChannel ->
83+
requiresChannel
8384
.anyRequest().requiresSecure()
84-
.and()
85+
)
8586
.portMapper(portMapper ->
8687
portMapper
8788
.http(543).mapsTo(123)
@@ -106,9 +107,10 @@ protected void configure(HttpSecurity http) throws Exception {
106107
customPortMapper.setPortMappings(Collections.singletonMap("543", "123"));
107108
// @formatter:off
108109
http
109-
.requiresChannel()
110-
.anyRequest().requiresSecure()
111-
.and()
110+
.requiresChannel(requiresChannel ->
111+
requiresChannel
112+
.anyRequest().requiresSecure()
113+
)
112114
.portMapper(portMapper ->
113115
portMapper
114116
.portMapper(customPortMapper)

0 commit comments

Comments
 (0)