The request was rejected because the URL contained a potentially malicious String “;“问题的正确解决姿势

The request was rejected because the URL contained a potentially malicious String “;“问题的正确解决姿势,第1张

问题的复盘

首先这个问题出现的时机是,当用户访问特定的连接(如http://localhost/index)时没有权限,被重定向到登录页面http://localhost/login。为了登录成功后再跳转到目标访问的页面http://localhost/index,Spring Security会在Cookie中存一个信息,标记一为一个jsessionid。重定向时Servlet容器,也就是tomcat之类的会把jsessionid编码到重定向url,也就是http://localhost/login;jsessionid=xxxxxxxxxx。这种请求会被Spring Security的StrictHttpFirewall拦截,引发进而The request was rejected because the URL contained a potentially malicious String ";"错误。

安全策略

OWASP指出在URL中暴露jsessionid是非常危险的举动,可能导致会话固定攻击,因此不建议上述的行为。

解决方案

目前有两种解决方案。

允许url携带jsessionid

这种在网上很多,如果浏览器的cookie被禁用或者你的应用可以容忍上述安全漏洞你可以在Spring Security中采取这种方式:

httpSecurity
  .sessionManagement()
    .enableSessionUrlRewriting(true);

这种不建议使用。

修改servlet容器的会话机制

在 Spring Boot 中配置 Tomcat 的跟踪模式:

server.servlet.session.tracking-modes=cookie

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/924654.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存