gpt4 book ai didi

spring-mvc - Spring 5.0.3 RequestRejectedException : The request was rejected because the URL was not normalized

转载 作者:行者123 更新时间:2023-12-03 05:18:32 26 4
gpt4 key购买 nike

不确定这是否是 Spring 5.0.3 的一个错误,或者是一个新功能来解决我的问题。

升级后,我收到此错误。有趣的是,这个错误仅出现在我的本地计算机上。使用 HTTPS 协议(protocol)的测试环境中的相同代码运行良好。

继续...

我收到此错误的原因是因为我用于加载结果 JSP 页面的 URL 是 /location/thisPage.jsp。评估代码 request.getRequestURI() 给出结果 /WEB-INF/somelocation//location/thisPage.jsp。如果我将 JSP 页面的 URL 固定到此 location/thisPage.jsp,则一切正常。

所以我的问题是,我是否应该从代码中的 JSP 路径中删除 / ,因为这是 future 所需要的。或者 Spring 引入了一个错误,因为我的机器和测试环境之间的唯一区别是协议(protocol) HTTP 与 HTTPS。

 org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL was not normalized.
at org.springframework.security.web.firewall.StrictHttpFirewall.getFirewalledRequest(StrictHttpFirewall.java:123)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:194)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:186)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)

最佳答案

Spring Security Documentation在请求中提到了阻止//的原因。

For example, it could contain path-traversal sequences (like /../) or multiple forward slashes (//) which could also cause pattern-matches to fail. Some containers normalize these out before performing the servlet mapping, but others don’t. To protect against issues like these, FilterChainProxy uses an HttpFirewall strategy to check and wrap the request. Un-normalized requests are automatically rejected by default, and path parameters and duplicate slashes are removed for matching purposes.

所以有两种可能的解决方案 -

  1. 删除双斜线(首选方法)
  2. 通过使用以下代码自定义 StrictHttpFirewall,在 Spring Security 中允许//。

第 1 步创建允许 URL 中包含斜线的自定义防火墙。

@Bean
public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
StrictHttpFirewall firewall = new StrictHttpFirewall();
firewall.setAllowUrlEncodedSlash(true);
return firewall;
}

第2步然后在websecurity中配置这个bean

@Override
public void configure(WebSecurity web) throws Exception {
//@formatter:off
super.configure(web);
web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
....
}

第2步是可选步骤,Spring Boot只需要声明一个HttpFirewall类型的bean,它就会在过滤器链中自动配置它。

Spring Security 5.4 更新

在 Spring security 5.4 及更高版本 (Spring Boot >= 2.4.0) 中,我们可以通过创建以下 bean 来消除太多提示请求被拒绝的日志。

import org.springframework.security.web.firewall.RequestRejectedHandler;
import org.springframework.security.web.firewall.HttpStatusRequestRejectedHandler;

@Bean
RequestRejectedHandler requestRejectedHandler() {
return new HttpStatusRequestRejectedHandler();
}

关于spring-mvc - Spring 5.0.3 RequestRejectedException : The request was rejected because the URL was not normalized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48453980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com