最近LZ給項目框架升級, 從Spring1.x升級到Spring2.x, 在這裏就很少贅述兩個版本之間的區別以及升級的緣由。java
關於升級過程當中踩的坑,在其餘博文中會作比較詳細的記錄,以便給讀者參考,不要掉進一樣的坑裏。 這裏咱們討論一個關於URL中包含雙斜槓被攔截的問題。web
升級框架以後,測試一個功能時,發現報錯Http 500, 第一時間懷疑是後臺功能報錯。打印後臺錯誤日誌,發現報錯信息:The request was rejected because the URL was not normalized。spring
以後與升級前相同環境對比發現,相同的功能, 升級以後,URL中包含雙斜槓。框架
通過對比不一樣和錯誤信息,初步定位問題出在URL上。查詢資料得知,Spring Security 在高版本中增長了StrictHttpFirewall類,對URL校驗更加嚴格。因而查看源碼:ide
private static boolean isNormalized(String path) {
if (path == null) {
return true;
} else if (path.indexOf("//") > -1) {
return false;
} else {
int i;
for(int j = path.length(); j > 0; j = i) {
i = path.lastIndexOf(47, j - 1);
int gap = j - i;
if (gap == 2 && path.charAt(i + 1) == '.') {
return false;
}
if (gap == 3 && path.charAt(i + 1) == '.' && path.charAt(i + 2) == '.') {
return false;
}
}
return true;
}
}
方法一:修改項目中出現「//」雙斜槓的URL路徑,哈哈測試
方法二:自定義FireWall方式容許URL出現雙斜槓「//」url
參考:Spring 5.0.3 RequestRejectedException: The request was rejected because the URL was not normalized日誌
https://stackoverflow.com/questions/48453980/spring-5-0-3-requestrejectedexception-the-request-was-rejected-because-the-url/49116274code
@Bean public HttpFirewall allowUrlEncodedSlashHttpFirewall() { StrictHttpFirewall firewall = new StrictHttpFirewall(); firewall.setAllowUrlEncodedSlash(true); return firewall; }
2.在WebSecurity中配置這個bean。orm
@Override public void configure(WebSecurity web) throws Exception { //@formatter:off super.configure(web); web.httpFirewall(allowUrlEncodedSlashHttpFirewall()); .... }
至此,問題解決。