我有一个GET方法
@RequestMapping( value = "/path/{field}", method = RequestMethod.GET ) public void get(@PathVariable String field) { }
字段可以包含斜线,例如“ some / thing / else”,这导致找不到路径。它甚至可能类似于“ //////////////”。一些例子:
www.something.com/path/field //works www.something.com/path/some/thing www.something.com/path///////////
我已经尝试过使用{field:。*}并使用%2F转义斜线,但仍然无法达到该方法。一些帮助?
对于Spring Boot,我已经解决了这个问题。首先,您必须配置Spring以允许编码的斜杠。
@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()); } }
然后,您需要从嵌入式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); } }
尽管这可行,但是最好的方法是仅使用查询参数。