【问题标题】:Spring & Docker: request was rejected because URL was not normalizedSpring & Docker:请求被拒绝,因为 URL 未规范化
【发布时间】:2019-07-28 08:58:26
【问题描述】:

我有两个需要相互通信的 docker 容器。一个是前端的 nginx 容器,它需要与另一个容器中的 Spring 后端通信。在 Docker 外部运行时通信工作正常,但是当我对项目进行 dockerize 时,尝试将任何请求从前端发送到后端时出现以下错误:

 org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL was not normalized.

来自 Spring 的 StrictHttpFirewall。

nginx.conf

load_module "modules/ngx_http_perl_module.so";
env HELIUM_LOCATION;

http {
    perl_set $helium_location 'sub { return $ENV{"HELIUM_LOCATION"}; }';

    server {
        listen 8000;
        root /usr/share/nginx/html;
        include /etc/nginx/mime.types;
        client_max_body_size 10M;

        location /api {
            rewrite ^/api(.*) /$1 break;
            proxy_set_header X-Forwarded-Host $host:$server_port;
            proxy_set_header X-Forwarded-Prefix /api;
            proxy_pass http://$helium_location;
        }

        location /health {
             default_type application/json;
             return 200 '{"status": "UP"}';
        }

        location / {
            try_files $uri $uri/ /index.html;
        }
    }
 }

Spring boot 版本为 2.1,nginx 容器为 nginx:1.11.8-alpine。这在使用 Spring boot 1.5.7 时有效,那么 Spring 处理这些请求的方式是否发生了变化?

如果有任何其他信息有助于解决此问题,请告诉我,我会尽我所能为您获取。谢谢!

【问题讨论】:

    标签: spring spring-boot docker nginx


    【解决方案1】:

    来自Spring Framework Documentation

    例如,它可能包含路径遍历序列(如 /../)或 多个正斜杠 (//),这也可能导致模式匹配 失败。

    您在此重写中添加了一个额外的斜线

    location /api {
        rewrite ^/api(.*) /$1 break;
        ...
    }
    

    在日志上我可以看到这个

    127.0.0.1 - - [06/Mar/2019:16:22:15 +0000] "GET //something HTTP/1.0" 200 612 "-" "curl/7.52.1"
    172.17.0.1 - - [06/Mar/2019:16:22:15 +0000] "GET /api/something HTTP/1.1" 200 612 "-" "curl/7.52.1"
    

    改成

    rewrite ^/api(.*) $1 break;
    

    你可以通过nginx -s reload重新加载配置

    现在不添加额外的斜线

    127.0.0.1 - - [06/Mar/2019:16:27:22 +0000] "GET /something HTTP/1.0" 200 612 "-" "curl/7.52.1"
    172.17.0.1 - - [06/Mar/2019:16:27:22 +0000] "GET /api/something HTTP/1.1" 200 612 "-" "curl/7.52.1"
    

    【讨论】:

    • 感谢您的回复!我从日志中抓取了一行,看起来这不是问题所在:sms_qa.phosphorus | 10.1.2.22 - - [06/Mar/2019:18:01:17 +0000] "POST /api/auth HTTP/1.1" 401 0 "10.1.1.101:81/login" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" 沿线某处必须已经处理
    • 您是否尝试删除该重写中的多余斜线?
    • 是的。当我这样做时,请求并没有到达后端。显然,该应用程序依赖于沿线某处的斜线。
    猜你喜欢
    • 2018-07-05
    • 2019-06-26
    • 2019-08-17
    • 2018-08-24
    • 2018-11-30
    • 1970-01-01
    • 2019-03-05
    • 2022-10-23
    • 2017-06-18
    相关资源
    最近更新 更多