Spring - slash character in GET url
I have a GET method
@RequestMapping(
value = "/path/{field}",
method = RequestMethod.GET
)
public void get(@PathVariable String field) {
}
The field may contain forward slashes such as "some / thing / else" resulting in no path found. It could be something like "//////////////". Some examples:
www.something.com/path/field //works
www.something.com/path/some/thing
www.something.com/path///////////
I tried with {field :. *} and escaped the slash with% 2F, but still doesn't get to the method. Some help?
+3
source to share
3 answers
I solved the problem, for Spring Boot. You must first configure Spring to allow encoded forward slashes.
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
DefaultHttpFirewall firewall = new DefaultHttpFirewall();
firewall.setAllowUrlEncodedSlash(true);
return firewall;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
}
}
Then you need to resolve it from embedded tomcat:
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
SpringApplication.run(Application.class, args);
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
While this works, the best way to do it is to simply use query parameters.
+2
source to share