fix: user's route was prior to the plugin's one (#2733)

This commit is contained in:
罗泽轩 2020-11-15 17:28:56 +08:00 committed by GitHub
parent 91d99452c5
commit 23a47b9eac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 5 deletions

View File

@ -365,8 +365,8 @@ function _M.http_access_phase()
api_ctx.global_rules = router.global_rules
end
if local_conf.apisix and local_conf.apisix.delete_uri_tail_slash then
local uri = api_ctx.var.uri
if local_conf.apisix and local_conf.apisix.delete_uri_tail_slash then
if str_byte(uri, #uri) == str_byte("/") then
api_ctx.var.uri = str_sub(api_ctx.var.uri, 1, #uri - 1)
core.log.info("remove the end of uri '/', current uri: ",
@ -374,10 +374,14 @@ function _M.http_access_phase()
end
end
local user_defined_route_matched = router.router_http.match(api_ctx)
if not user_defined_route_matched then
router.api.match(api_ctx)
if core.string.has_prefix(uri, "/apisix/") then
local matched = router.api.match(api_ctx)
if matched then
return
end
end
router.router_http.match(api_ctx)
local route = api_ctx.matched_route
if not route then

View File

@ -20,7 +20,10 @@ add_block_preprocessor(sub {
my ($block) = @_;
$block->set_value("no_error_log", "[error]");
if (!defined $block->request) {
$block->set_value("request", "GET /t");
}
$block;
});
@ -52,3 +55,107 @@ __DATA__
}
--- response_body
ok
=== TEST 2: define route for /*
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/consumers',
ngx.HTTP_PUT,
[[{
"username": "jack",
"plugins": {
"jwt-auth": {
"key": "user-key",
"secret": "my-secret-key"
}
}
}]])
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"jwt-auth": {}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/*"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 3: sign
--- request
GET /apisix/plugin/jwt/sign?key=user-key
--- response_body_like eval
qr/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.\w+.\w+/
=== TEST 4: delete /* and define route for /apisix/plugin/blah
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code = t('/apisix/admin/routes/1', "DELETE")
if code >= 300 then
ngx.status = code
return
end
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"jwt-auth": {}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/apisix/plugin/blah"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 5: hit
--- request
GET /apisix/plugin/blah
--- error_code: 401
--- response_body
{"message":"Missing JWT token in request"}