Spring - slash character in GET URL

Alex Parvan :

I have a GET method

@RequestMapping(
        value = "/path/{field}",
        method = RequestMethod.GET
)
public void get(@PathVariable String field) {
}

Field can contain slashes, like "some/thing/else" which results in the path not being found. It could even be something like "//////////////". Some examples:

www.something.com/path/field //works
www.something.com/path/some/thing
www.something.com/path///////////

I've tried with {field:.*} and escaping the slash with %2F but it still won't reach the method. Some help?

Alex Parvan :

I've solved the problem, for Spring Boot. First you have to configure Spring to allow encoded 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 allow it from the 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);
    }
}

Although this works, the best way to do it is to just use query parameters instead.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=471956&siteId=1