cll1778890283 commented on issue #12746: URL: https://github.com/apache/apisix/issues/12746#issuecomment-3521438674
> Hi [@cll1778890283](https://github.com/cll1778890283) > > In your plugin code (file `ale-key-auth-consumer.lua`), try changing this line: > > local key_auth_schema = require("custom-plugin.ale-key-auth-consumer.init") > to this: > > local key_auth_schema = require("apisix.plugins.custom-plugin.ale-key-auth-consumer.init") > If that still doesn't work, try changing the `config.yaml` for `extra_lua_path` to the one below (remove the escape character `\`): > > apisix: > extra_lua_path: "/usr/local/apisix/custom-plugin/?.lua" Hello, I tried your way and it still doesn't work. Please take a screenshot below Plugin code: local core = require("apisix.core") local consumer_mod = require("apisix.consumer") local key_auth_schema = require("apisix.plugins.custom-plugin.ale-key-auth-consumer.init") local plugin_name = "ale-key-auth-consumer" local _M = { type = "auth", -- 关键:声明为认证类型插件 !!!!! version = 0.1, priority = 2500, name = plugin_name, schema = key_auth_schema.schema, consumer_schema = key_auth_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 Subdirectory module code: 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 = { schema = schema, consumer_schema = consumer_schema } return _M Execute script: docker run -d --name apisix-custom -p 9080:9080 -p 9443:9443 -p 9180:9180 -p 9090:9090 --network apisix-network -v E:/apisix-project/code/apisix/conf/custom-config.yaml:/usr/local/apisix/conf/config.yaml -v E:/apisix-project/code/apisix/apisix/plugins/custom-plugin:/usr/local/apisix/custom-plugin/apisix/plugins apache/apisix:3.7.0-debian Local mounted container directory: <!-- Failed to upload "image.png" --> Error message: <!-- Failed to upload "image.png" --> config.yaml: apisix: extra_lua_path: "/usr/local/apisix/custom-plugin/?.lua" node_listen: 9080 enable_ipv6: false enable_control: true control: ip: "0.0.0.0" port: 9090 plugins: # plugin list (sorted by priority) - real-ip # priority: 23000 - ai # priority: 22900 #自定义插件 - ale-request-logger - ale-key-auth-no-consumer # priority: 2500 - ale-key-auth-consumer # priority: 2500 nginx_config: error_log_level: "warn" # 全局日志级别改为 info deployment: admin: allow_admin: - 0.0.0.0/0 admin_key: - name: "admin" key: edd1c9f034335f136f87ad84b625c8f1 role: admin - name: "viewer" key: 4054f7cf07e344346cd3f287985e76a2 role: viewer admin_listen: ip: 0.0.0.0 port: 9180 etcd: host: - "http://etcd:2379" timeout: 30 -- 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]
