You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx Lua配置:如何实现邮箱不包含指定域名时拦截访问

在Nginx Lua中实现基于邮箱域名的访问拦截

你的现有代码直接判断邮箱是否等于example.com,这完全不符合需求——我们要实现的是当邮箱不包含example.com时拦截访问,而非判断邮箱与域名完全相等。

解决方案

在Lua里可以用string.find()函数检测子串是否存在:如果函数返回nil,说明目标子串不存在,此时触发拦截逻辑。

修改后的代码:

-- 邮箱不包含example.com则拦截访问
if not string.find(res.id_token.email, 'example.com') then
  ngx.exit(ngx.HTTP_FORBIDDEN)
end

进阶优化(精确匹配邮箱后缀)

如果要避免类似testexample.com这类包含example.com但不是合法后缀的情况,可以用正则匹配邮箱结尾:

-- 精确匹配@example.com结尾的邮箱,否则拦截
if not string.find(res.id_token.email, '@example.com$') then
  ngx.exit(ngx.HTTP_FORBIDDEN)
end

这里的$是正则锚点,限定匹配字符串的结尾,确保邮箱是xxx@example.com的标准格式。

内容的提问来源于stack exchange,提问作者Ajay k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 13:50:21