Skip to content

Commit ad62905

Browse files
committed
Ensure that custom static resource locations end with /
Closes gh-9360
1 parent 5be5b13 commit ad62905

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ public String[] getStaticLocations() {
8686
}
8787

8888
public void setStaticLocations(String[] staticLocations) {
89-
this.staticLocations = staticLocations;
89+
this.staticLocations = appendSlashIfNecessary(staticLocations);
90+
}
91+
92+
private String[] appendSlashIfNecessary(String[] staticLocations) {
93+
String[] normalized = new String[staticLocations.length];
94+
for (int i = 0; i < staticLocations.length; i++) {
95+
String location = staticLocations[i];
96+
normalized[i] = location.endsWith("/") ? location : location + "/";
97+
}
98+
return normalized;
9099
}
91100

92101
public Resource getWelcomePage() {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 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.
@@ -18,7 +18,10 @@
1818

1919
import org.junit.Test;
2020

21+
import org.springframework.boot.testutil.Matched;
22+
2123
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.hamcrest.CoreMatchers.endsWith;
2225

2326
/**
2427
* Tests for {@link ResourceProperties}.
@@ -52,4 +55,17 @@ public void resourceChainDisabled() {
5255
assertThat(this.properties.getChain().getEnabled()).isFalse();
5356
}
5457

58+
@Test
59+
public void defaultStaticLocationsAllEndWithTrailingSlash() {
60+
assertThat(this.properties.getStaticLocations()).are(Matched.by(endsWith("/")));
61+
}
62+
63+
@Test
64+
public void customStaticLocationsAreNormalizedToEndWithTrailingSlash() {
65+
this.properties.setStaticLocations(new String[] { "/foo", "/bar", "/baz/" });
66+
assertThat(this.properties.getStaticLocations()).containsExactly("/foo/", "/bar/",
67+
"/baz/");
68+
69+
}
70+
5571
}

0 commit comments

Comments
 (0)