SpringBoot 整合升级 Spring Security 报错 【The request was rejected because the URL was not normalized】

目录

前言

发现问题

分析问题

解决问题

参考文档


前言

       公司在推项目框架升级, 从 Spring1.x 升级到 Spring2.x , LZ 在给服务升级的时候出现一个关于 URL 中包含双斜杠被拦截的问题。

发现问题

       升级框架之后,马上收到短信和邮件报警,查看 nginx 日志发现报500的全是 URL 中包含双斜杠的请求,通过 nginx 的 traceId 定位到具体的应用服务机器,发现报错如下:

分析问题

       从报错 “The request was rejected because the URL was not normalized” 字面意思可以猜测一下,应该是 URL 中多了一个斜杠。为了验证我们的猜想我们通过报错的堆栈信息进入 StrictHttpFirewall 类源码分析:

解决问题

方法一:修改项目中出现 “//” 双斜杠的 URL 路径。

方法二:自定义 FireWall 方式允许 URL 出现双斜杠 “//”。

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());
....
}

参考文档

       https://stackoverflow.com/questions/48453980/spring-5-0-3-requestrejectedexception-the-request-was-rejected-because-the-url/49116274 

发布了67 篇原创文章 · 获赞 64 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/jack1liu/article/details/103833280