Kong自定义Lua插件无法向Spring Boot REST API正常发送HTTP请求
Kong自定义Lua插件转发请求至Spring Boot API时的I/O错误问题
我被这个问题困扰数周了。我为Kong编写了一个自定义Lua插件,用于将Kong接收到的请求转发至自定义认证服务,以下是Handler.lua代码:
-- Handler.lua local http = require "resty.http" local utils = require "kong.tools.utils" local cjson = require("cjson") local MessageForwarder = { VERSION = "1.0", PRIORITY = 1000, } local function forward_message(conf, request_method, request_path, request_headers, request_body) -- Make HTTP Request local httpc = http.new() local res, err = httpc:request_uri(conf.forward_host .. request_path, { method = request_method, headers = request_headers, body = request_body, ssl_verify = false }) -- Checking response if not res then kong.log.err("Failed to forward request: ", err) return kong.response.exit(500) end if res.status ~= 200 then kong.log.err("Responded with status: ",res.status) return kong.response.exit(500) end return true -- all is well end local function forward_message_new(conf, request_method, request_path, request_headers, request_body) local server = assert(socket.bind("*", 0)) end function MessageForwarder:access(conf) local request_path = kong.request.get_raw_path() local request_method = kong.request.get_method() local request_headers = kong.request.get_headers() local raw_request_body if raw_request_method ~= "GET" then raw_request_body = kong.request.get_raw_body() end local request_body = cjson.decode(raw_request_body) local auth_path = conf.auth_token_b2b_endpoint local bool = forward_message(conf, request_method, request_path, request_headers, request_body) return true end return MessageForwarder
接收该转发请求的Spring Boot REST API日志中返回如下错误:
WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Ignoring exception, response committed already: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message WARN 20440 --- [nio-8010-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message]
我已经通过Postman、Insomnia、其他Spring Boot应用及Ajax测试过这个Spring Boot API,均能正常工作,唯有该Kong自定义Lua插件调用出现问题。我怀疑自己在使用resty.http时未遵循某些规则,恳请各位提供解决方案。
补充:我参考了Kong博客的自定义认证与授权框架教程来适配需求。
内容的提问来源于stack exchange,提问作者Mehdi.
相关产品推荐
相关产品推荐

