cll1778890283 opened a new issue, #12741:
URL: https://github.com/apache/apisix/issues/12741

   ### Description
   
   source code:
   ---
   --- Generated by EmmyLua(https://github.com/EmmyLua)
   --- Created by Administrator.
   --- DateTime: 2025/11/9 10:46
   ---
   local core = require("apisix.core")
   local consumer_mod = require("apisix.consumer")
   
   -- 插件元信息及配置 schema
   local plugin_name = "ale-key-auth-consumer"
   
   -- 路由的schema
   local schema = {
       type = "object",
       properties = {
           header = {
               type = "string",
               default = "apikey",
           },
           query = {  -- 新增:从查询参数获取密钥的字段名(默认 apikey)
               type = "string",
               default = "apikey",
           }
       }
   }
   
   -- 消费者的schema
   local consumer_schema = {
       type = "object",
       properties = {
           key = {
               type = "string",
               minLength = 1,
           },
       },
       required = {"key"}
   }
   
   local _M = {
       version = 0.1,
       priority = 2500,
       name = plugin_name,
       schema = schema,
       consumer_schema = consumer_schema,
   }
   
   function _M.check_schema(conf, schema_type)
       if schema_type == core.schema.TYPE_CONSUMER then
           return core.schema.check(consumer_schema, conf)
       else
           return core.schema.check(schema, conf)
       end
   end
   
   function _M.rewrite(conf, ctx)
       --优先从请求头中获取
       local from_header = true
       local key = core.request.header(ctx, conf.header)
       core.log.warn("请求头中获取key: ", key)
   
       if not key then
           local uri_args = core.request.get_uri_args(ctx) or {}
           key = uri_args[conf.query]
           from_header = false
       end
   
       if not key then
           return 401, {message = "Missing API key found in request"}
       end
   
       local consumer_conf = consumer_mod.plugin(plugin_name)
       local consumer_conf_str = consumer_conf and 
core.json.encode(consumer_conf) or "nil"
       core.log.warn("获取消费者配置: " .. consumer_conf_str .. ",插件名称:" .. 
plugin_name)
       if not consumer_conf then
           return 401, {message = "Missing related consumer"}
       end
   
       local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, 
"key")
       local consumer = consumers[key]
       if not consumer then
           return 401, {message = "Invalid API key in request"}
       end
       core.log.warn("consumer: ", core.json.delay_encode(consumer))
   
       consumer_mod.attach_consumer(ctx, consumer, consumer_conf)
       core.log.warn("hit ale-key-auth rewrite")
   end
   
   return _M
   
   
   apisix dashboard:Successfully created consumer and bound plugin
   
     local consumer_conf = consumer_mod.plugin(plugin_name)
    local consumer_conf_str = consumer_conf and core.json.encode(consumer_conf) 
or "nil"
    core.log.warn("获取消费者配置: " .. consumer_conf_str .. ",插件名称:" .. plugin_name)
   
    We cannot find consumer configuration information based on the plugin here
   
   error log info:
   2025/11/09 09:17:49 [warn] 55#55: *273968 [lua] 
ale-key-auth-consumer.lua:59: phase_func(): 请求头中获取key: 000, client: 172.18.0.1, 
server: _, request: "GET /api/hello HTTP/1.1", host: "172.28.160.1:9080"
   
   2025/11/09 09:17:49 [warn] 55#55: *273968 [lua] 
ale-key-auth-consumer.lua:73: phase_func(): 获取消费者配置: 
nil,插件名称:ale-key-auth-consumer, client: 172.18.0.1, server: _, request: "GET 
/api/hello HTTP/1.1", host: "172.28.160.1:9080"
   
   
   ### Environment
   
   - APISIX version (run `apisix version`):3.7
   - Operating system (run `uname -a`):
   - OpenResty / Nginx version (run `openresty -V` or `nginx -V`):
   - etcd version, if relevant (run `curl 
http://127.0.0.1:9090/v1/server_info`):3.5.9
   - APISIX Dashboard version, if relevant:3.0
   - Plugin runner version, for issues related to plugin runners:
   - LuaRocks version, for installation issues (run `luarocks --version`):
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to