|
|
@ -21,13 +21,18 @@ import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.catalina.Valve;
|
|
|
|
|
|
|
|
import org.apache.catalina.valves.RemoteIpValve;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.springframework.beans.MutablePropertyValues;
|
|
|
|
import org.springframework.beans.MutablePropertyValues;
|
|
|
|
import org.springframework.boot.bind.RelaxedDataBinder;
|
|
|
|
import org.springframework.boot.bind.RelaxedDataBinder;
|
|
|
|
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
|
|
|
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
|
|
|
|
|
|
|
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.core.IsInstanceOf.instanceOf;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
@ -37,6 +42,7 @@ import static org.mockito.Mockito.verify;
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Dave Syer
|
|
|
|
* @author Dave Syer
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
|
|
|
* @author Andy Wilkinson
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class ServerPropertiesTests {
|
|
|
|
public class ServerPropertiesTests {
|
|
|
|
|
|
|
|
|
|
|
@ -84,13 +90,15 @@ public class ServerPropertiesTests {
|
|
|
|
map.put("server.tomcat.access_log_pattern", "%h %t '%r' %s %b");
|
|
|
|
map.put("server.tomcat.access_log_pattern", "%h %t '%r' %s %b");
|
|
|
|
map.put("server.tomcat.protocol_header", "X-Forwarded-Protocol");
|
|
|
|
map.put("server.tomcat.protocol_header", "X-Forwarded-Protocol");
|
|
|
|
map.put("server.tomcat.remote_ip_header", "Remote-Ip");
|
|
|
|
map.put("server.tomcat.remote_ip_header", "Remote-Ip");
|
|
|
|
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
|
|
|
map.put("server.tomcat.internal_proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
|
|
|
|
map));
|
|
|
|
bindProperties(map);
|
|
|
|
assertEquals("%h %t '%r' %s %b", this.properties.getTomcat()
|
|
|
|
assertEquals("%h %t '%r' %s %b", this.properties.getTomcat()
|
|
|
|
.getAccessLogPattern());
|
|
|
|
.getAccessLogPattern());
|
|
|
|
assertEquals("Remote-Ip", this.properties.getTomcat().getRemoteIpHeader());
|
|
|
|
assertEquals("Remote-Ip", this.properties.getTomcat().getRemoteIpHeader());
|
|
|
|
assertEquals("X-Forwarded-Protocol", this.properties.getTomcat()
|
|
|
|
assertEquals("X-Forwarded-Protocol", this.properties.getTomcat()
|
|
|
|
.getProtocolHeader());
|
|
|
|
.getProtocolHeader());
|
|
|
|
|
|
|
|
assertEquals("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", this.properties.getTomcat()
|
|
|
|
|
|
|
|
.getInternalProxies());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -112,8 +120,7 @@ public class ServerPropertiesTests {
|
|
|
|
public void testCustomizeUriEncoding() throws Exception {
|
|
|
|
public void testCustomizeUriEncoding() throws Exception {
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
map.put("server.tomcat.uriEncoding", "US-ASCII");
|
|
|
|
map.put("server.tomcat.uriEncoding", "US-ASCII");
|
|
|
|
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
|
|
|
bindProperties(map);
|
|
|
|
map));
|
|
|
|
|
|
|
|
assertEquals("US-ASCII", this.properties.getTomcat().getUriEncoding());
|
|
|
|
assertEquals("US-ASCII", this.properties.getTomcat().getUriEncoding());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -121,9 +128,66 @@ public class ServerPropertiesTests {
|
|
|
|
public void testCustomizeTomcatHeaderSize() throws Exception {
|
|
|
|
public void testCustomizeTomcatHeaderSize() throws Exception {
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
map.put("server.tomcat.maxHttpHeaderSize", "9999");
|
|
|
|
map.put("server.tomcat.maxHttpHeaderSize", "9999");
|
|
|
|
|
|
|
|
bindProperties(map);
|
|
|
|
|
|
|
|
assertEquals(9999, this.properties.getTomcat().getMaxHttpHeaderSize());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void disableTomcatRemoteIpValve() throws Exception {
|
|
|
|
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
|
|
|
|
map.put("server.tomcat.remote_ip_header", "");
|
|
|
|
|
|
|
|
map.put("server.tomcat.protocol_header", "");
|
|
|
|
|
|
|
|
bindProperties(map);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
|
|
|
|
|
|
|
|
this.properties.customize(container);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(0, container.getValves().size());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void defaultTomcatRemoteIpValve() throws Exception {
|
|
|
|
|
|
|
|
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
|
|
|
|
|
|
|
|
this.properties.customize(container);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, container.getValves().size());
|
|
|
|
|
|
|
|
Valve valve = container.getValves().iterator().next();
|
|
|
|
|
|
|
|
assertThat(valve, instanceOf(RemoteIpValve.class));
|
|
|
|
|
|
|
|
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
|
|
|
|
|
|
|
|
assertEquals("x-forwarded-proto", remoteIpValve.getProtocolHeader());
|
|
|
|
|
|
|
|
assertEquals("x-forwarded-for", remoteIpValve.getRemoteIpHeader());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String expectedInternalProxies = "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 10/8
|
|
|
|
|
|
|
|
+ "192\\.168\\.\\d{1,3}\\.\\d{1,3}|" // 192.168/16
|
|
|
|
|
|
|
|
+ "169\\.254\\.\\d{1,3}\\.\\d{1,3}|" // 169.254/16
|
|
|
|
|
|
|
|
+ "127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; // 127/8
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedInternalProxies, remoteIpValve.getInternalProxies());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void customTomcatRemoteIpValve() throws Exception {
|
|
|
|
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
|
|
|
|
map.put("server.tomcat.remote_ip_header", "x-my-remote-ip-header");
|
|
|
|
|
|
|
|
map.put("server.tomcat.protocol_header", "x-my-protocol-header");
|
|
|
|
|
|
|
|
map.put("server.tomcat.internal_proxies", "192.168.0.1");
|
|
|
|
|
|
|
|
bindProperties(map);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
|
|
|
|
|
|
|
|
this.properties.customize(container);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, container.getValves().size());
|
|
|
|
|
|
|
|
Valve valve = container.getValves().iterator().next();
|
|
|
|
|
|
|
|
assertThat(valve, instanceOf(RemoteIpValve.class));
|
|
|
|
|
|
|
|
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
|
|
|
|
|
|
|
|
assertEquals("x-my-protocol-header", remoteIpValve.getProtocolHeader());
|
|
|
|
|
|
|
|
assertEquals("x-my-remote-ip-header", remoteIpValve.getRemoteIpHeader());
|
|
|
|
|
|
|
|
assertEquals("192.168.0.1", remoteIpValve.getInternalProxies());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void bindProperties(Map<String, String> map) {
|
|
|
|
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
|
|
|
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
|
|
|
map));
|
|
|
|
map));
|
|
|
|
assertEquals(9999, this.properties.getTomcat().getMaxHttpHeaderSize());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|