springboot中URL帶有斜槓的轉義字符%2F致使的400錯誤

背景:

今天項目上出現一個問題,是前端的GET請求url中帶有路徑參數,這個參數中有/這個特殊字符,在postman的url中已經轉移成了%2F,後端用的是springboot,並無收到這個請求,直接返回了400的錯誤前端

緣由:

聽說是tomcat默認是不支持轉義的,須要手動設置一下轉化,這個搜索tomcat的設置能夠找到,可是這個是springboot,有內置的tomcat,可是在yml中找不到相關的配置。spring

解決方法:

修改一下啓動類,加一個系統參數,重寫WebMvcConfigurerAdapter的configurePathMatch方法.apache

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) throws Exception {
     // step2: System.setProperty(
"org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true"); SpringApplication.run(Application.class, args); }
  // step1: @Override
public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setUrlDecode(false); configurer.setUrlPathHelper(urlPathHelper); } }

4. 本地tomcat 中該如何設置呢? (本例是修改SpringBoot 內嵌 的 tomcat 配置)

在tomcat目錄 /conf/catalina.properties  文件末尾添加下列內容後端

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

重啓tomcat成功解決問題springboot

5. 其餘經常使用特殊字符的url轉義用法以下:

符號 URL中轉義結果 轉義碼
+ URL 中 + 號表示空格 %2B
空格 URL中的空格能夠用+號或者編碼 %20
/ 分隔目錄和子目錄 %2F
? 分隔實際的URL和參數 %3F
% 指定特殊字符 %25
# 表示書籤 %23
& URL中指定的參數間的分隔符 %26
= URL中指定參數的值 %3D
相關文章
相關標籤/搜索