Skip to content

Commit aa0f788

Browse files
okohubrwinch
authored andcommitted
Add RedirectStrategy customization to ChannelSecurityConfigurer for RetryWith classes
1 parent 0c20156 commit aa0f788

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -30,7 +30,9 @@
3030
import org.springframework.security.config.annotation.SecurityConfigurer;
3131
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
3232
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
33+
import org.springframework.security.web.DefaultRedirectStrategy;
3334
import org.springframework.security.web.PortMapper;
35+
import org.springframework.security.web.RedirectStrategy;
3436
import org.springframework.security.web.access.channel.ChannelDecisionManagerImpl;
3537
import org.springframework.security.web.access.channel.ChannelProcessingFilter;
3638
import org.springframework.security.web.access.channel.ChannelProcessor;
@@ -75,6 +77,7 @@
7577
*
7678
* @param <H> the type of {@link HttpSecurityBuilder} that is being configured
7779
* @author Rob Winch
80+
* @author Onur Kagan Ozcan
7881
* @since 3.2
7982
*/
8083
public final class ChannelSecurityConfigurer<H extends HttpSecurityBuilder<H>>
@@ -86,6 +89,8 @@ public final class ChannelSecurityConfigurer<H extends HttpSecurityBuilder<H>>
8689

8790
private List<ChannelProcessor> channelProcessors;
8891

92+
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
93+
8994
private final ChannelRequestMatcherRegistry REGISTRY;
9095

9196
/**
@@ -123,9 +128,11 @@ private List<ChannelProcessor> getChannelProcessors(H http) {
123128
if (portMapper != null) {
124129
RetryWithHttpEntryPoint httpEntryPoint = new RetryWithHttpEntryPoint();
125130
httpEntryPoint.setPortMapper(portMapper);
131+
httpEntryPoint.setRedirectStrategy(this.redirectStrategy);
126132
insecureChannelProcessor.setEntryPoint(httpEntryPoint);
127133
RetryWithHttpsEntryPoint httpsEntryPoint = new RetryWithHttpsEntryPoint();
128134
httpsEntryPoint.setPortMapper(portMapper);
135+
httpsEntryPoint.setRedirectStrategy(this.redirectStrategy);
129136
secureChannelProcessor.setEntryPoint(httpsEntryPoint);
130137
}
131138
insecureChannelProcessor = postProcess(insecureChannelProcessor);
@@ -185,6 +192,17 @@ public ChannelRequestMatcherRegistry channelProcessors(List<ChannelProcessor> ch
185192
return this;
186193
}
187194

195+
/**
196+
* Sets the {@link RedirectStrategy} instances to use in
197+
* {@link RetryWithHttpEntryPoint} and {@link RetryWithHttpsEntryPoint}
198+
* @param redirectStrategy
199+
* @return the {@link ChannelSecurityConfigurer} for further customizations
200+
*/
201+
public ChannelRequestMatcherRegistry redirectStrategy(RedirectStrategy redirectStrategy) {
202+
ChannelSecurityConfigurer.this.redirectStrategy = redirectStrategy;
203+
return this;
204+
}
205+
188206
/**
189207
* Return the {@link SecurityBuilder} when done using the
190208
* {@link SecurityConfigurer}. This is useful for method chaining.

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -16,6 +16,11 @@
1616

1717
package org.springframework.security.config.annotation.web.configurers;
1818

19+
import java.io.IOException;
20+
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
1924
import org.junit.jupiter.api.Test;
2025
import org.junit.jupiter.api.extension.ExtendWith;
2126

@@ -27,6 +32,8 @@
2732
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2833
import org.springframework.security.config.test.SpringTestContext;
2934
import org.springframework.security.config.test.SpringTestContextExtension;
35+
import org.springframework.security.web.PortMapperImpl;
36+
import org.springframework.security.web.RedirectStrategy;
3037
import org.springframework.security.web.access.channel.ChannelDecisionManagerImpl;
3138
import org.springframework.security.web.access.channel.ChannelProcessingFilter;
3239
import org.springframework.security.web.access.channel.InsecureChannelProcessor;
@@ -44,6 +51,7 @@
4451
*
4552
* @author Rob Winch
4653
* @author Eleftheria Stein
54+
* @author Onur Kagan Ozcan
4755
*/
4856
@ExtendWith(SpringTestContextExtension.class)
4957
public class ChannelSecurityConfigurerTests {
@@ -93,6 +101,12 @@ public void requestWhenRequiresChannelConfiguredInLambdaThenRedirectsToHttps() t
93101
this.mvc.perform(get("/")).andExpect(redirectedUrl("https://localhost/"));
94102
}
95103

104+
@Test
105+
public void requestWhenRequiresChannelConfiguredWithUrlRedirectThenRedirectsToUrlWithHttps() throws Exception {
106+
this.spring.register(RequiresChannelWithTestUrlRedirectStrategy.class).autowire();
107+
this.mvc.perform(get("/")).andExpect(redirectedUrl("https://localhost/test"));
108+
}
109+
96110
@EnableWebSecurity
97111
static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter {
98112

@@ -155,4 +169,35 @@ protected void configure(HttpSecurity http) throws Exception {
155169

156170
}
157171

172+
@EnableWebSecurity
173+
static class RequiresChannelWithTestUrlRedirectStrategy extends WebSecurityConfigurerAdapter {
174+
175+
@Override
176+
protected void configure(HttpSecurity http) throws Exception {
177+
// @formatter:off
178+
http
179+
.portMapper()
180+
.portMapper(new PortMapperImpl())
181+
.and()
182+
.requiresChannel()
183+
.redirectStrategy(new TestUrlRedirectStrategy())
184+
.anyRequest()
185+
.requiresSecure();
186+
// @formatter:on
187+
}
188+
189+
}
190+
191+
static class TestUrlRedirectStrategy implements RedirectStrategy {
192+
193+
@Override
194+
public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url)
195+
throws IOException {
196+
String redirectUrl = url + "test";
197+
redirectUrl = response.encodeRedirectURL(redirectUrl);
198+
response.sendRedirect(redirectUrl);
199+
}
200+
201+
}
202+
158203
}

0 commit comments

Comments
 (0)