Languages
[Edit]
EN

Spring Boot 2 - enable encoded slash support in path segments (/ encoding as %2F, e.g. /path/to%2Fendpoint)

4 points
Created by:
Zeeshan-Peel
490

In this short article, we would like to show how in Spring Boot 2 enable encoded slashes support in path segments.

By default, it is not permitted to use %2F in path segments what was described here.

 

Solution

It is necessary to set:

org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true

In the below, we use used System.setProperty() to set the above flag:

package example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SpringBootWebApplication.class);
	}

	public static void main(String[] args) throws Exception {

        // allows to use %2F in path segments
        //
		System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");

		SpringApplication.run(SpringBootWebApplication.class, args);
	}
}

It is necessary to add path match configuration:

package example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

@Configuration
public class SpringApplicationConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {

        UrlPathHelper helper = new UrlPathHelper();
        helper.setUrlDecode(false);

        configurer.setUrlPathHelper(helper);
    }
}

See also

  1. Spring Boot 2 - request mapping with wildcard parameter (/path/**)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join