NGINX - lua-resty-openidc - 无法访问POST的API URL

使用已配置lua-resty-openidc的Openresty NGINX。

api.conf

location /api/postsomething {
    limit_except POST {
        deny all;
    }
    error_page 403 = @405; # 将响应从“403 (禁止)”转换为“405 (方法不允许)”
    set $upstream api_dev;
    rewrite ^ /_apipost last;
}

location = /_apipost {
    internal;
    set $api_name "someapi";
    access_by_lua_file /etc/nginx/oauth_authenticate.lua;
    proxy_pass https://$upstream$request_uri;
}

oauth_authenticate.lua

local opts = {
        redirect_uri="https://<ipd>/oauth_callback",
        discovery="https://<idp.ip>/.well-known/openid-configuration",
        client_id="client_id_test",
        client_secret="",
        scope="openid",
        ssl_verify="no"
}
local res, err =require("resty.openidc").authenticate(opts)

if err then
        ngx.status = 500
        ngx.say(err)
        ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end

local jwt = require "resty.jwt"
local jwt_obj = jwt:load_jwt(res.access_token)

-- 解析上方使用的访问令牌并将其打印到日志中
local cjson = require "cjson"
ngx.log(ngx.DEBUG, "此为访问令牌:\n " .. cjson.encode(jwt_obj.payload))

-- 设置包含用户信息的头部:这将覆盖任何现有的头部
-- 但也抹掉(!)这些头部以防没有在令牌中提供值
ngx.req.set_header("X-USER", res.id_token.sub)

现在在Postman中调用后端API的结果如下URL:

POST > https://nginx_ip/api/postsomething

结果:

<!-- 模板名称:form.autopost.template.html -->

<html>

<head>
    <title>提交表单</title>
</head>

<body onload="javascript:document.forms[0].submit()">
    <noscript>
        <p>
            <strong>注意:</strong>由于您的浏览器不支持JavaScript,您必须按一下“继续”按钮才能继续。
        </p>
    </noscript>
    <form method="post" action="https://<idp_ip>/idp/SSO.saml2">
        <input type="hidden" name="SAMLRequest" value="ddfdfdfdf3Q+"/>
        <input type="hidden" name="RelayState" value="fdfergerger"/>
        <noscript><input type="submit" value="继续"/></noscript>
    </form>
</body>

</html>

如何使用OIDC模块lua保护API(POST调用)?如果我在浏览器中发出相同的请求,会要求我进行用户身份验证,而浏览器的结果是这样的:

405 (方法不允许)

如果我创建一个HTML表单POST到https://nginx_ip/api/postsomething,结果是405。为什么?请帮忙。

点赞