EN
Spring Boot 2 - enable encoded backslash support in path segments (\ encoding as %5C, e.g. /path/to%5Cendpoint)
9 points
In this short article, we would like to show how in Spring Boot 2 enable encoded backslashes support in path segments.
By default, it is not permitted to use %5C
in path segments what was described here.
From Tomcat documentation:
Property | Description |
org.apache.catalina.connector. CoyoteAdapter.ALLOW_BACKSLASH | If this is true the \ character will be permitted as a path delimiter. If not specified, the default value of false will be used. |
It is necessary to set:
xxxxxxxxxx
1
org.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH=true
In the below, we use used System.setProperty()
to set the above flag:
xxxxxxxxxx
1
package example;
2
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
6
import org.springframework.boot.builder.SpringApplicationBuilder;
7
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
8
9
10
public class SpringBootWebApplication extends SpringBootServletInitializer {
11
12
13
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
14
return application.sources(SpringBootWebApplication.class);
15
}
16
17
public static void main(String[] args) throws Exception {
18
19
// allows to use %5C in path segments.
20
//
21
System.setProperty("org.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH", "true");
22
23
SpringApplication.run(SpringBootWebApplication.class, args);
24
}
25
}
It is necessary to add path match configuration:
xxxxxxxxxx
1
package example.config;
2
3
import org.springframework.context.annotation.Configuration;
4
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
5
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6
import org.springframework.web.util.UrlPathHelper;
7
8
9
public class SpringApplicationConfig implements WebMvcConfigurer {
10
11
12
public void configurePathMatch(PathMatchConfigurer configurer) {
13
14
UrlPathHelper helper = new UrlPathHelper();
15
helper.setUrlDecode(false);
16
17
configurer.setUrlPathHelper(helper);
18
}
19
}