我有一個 nginx 配置,其中使用 Lua 代碼我想阻止除 one( example.com) 之外的所有域。由于我使用的是 Azure,因此我無法從 IDToken 中獲取域值,因此我使用的是電子郵件([email protected]因為電子郵件還包含域名)。
如果用戶電子郵件不包含字串,則要求阻止所有訪問example.com
這是我到目前為止所擁有的,但它沒有不包含字串
if res.id_token.email ~= 'example.com' then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
它阻止所有電子郵件example.com
因為電子郵件可以是任何東西,它是一個應該使用“CONTAINS”的字串,所以它失敗了。如果條件不包含 example.com的解決方案是什么
uj5u.com熱心網友回復:
作為一種可能更有效的替代方法string.match,您還可以使用string.find(它回傳索引,因此不必構建匹配字串),并且還允許禁用模式匹配:
if not res.id_token.email:find("@example.com", 1, true) then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
請注意,您的問題要求包含,而不是結尾。
uj5u.com熱心網友回復:
這是長度@example.com
#'@example.com'
獲取電子郵件的后綴,長度為@example.com
res.id_token.email:sub(-#'@example.com')
與之比較@example.com
res.id_token.email:sub(-#'@example.com') ~= '@example.com'
uj5u.com熱心網友回復:
以下作業:
if not string.match(res.id_token.email, "@example.com") then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
發現string.match可以找到一個字串來匹配,我not在它前面加了一個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529221.html
標籤:nginxlua
