Nginx可以通过多种方式实现基于用户角色的访问控制。以下是几种常见的方法:
location /admin {
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/.htpasswd;
# 可以结合IP限制
allow 192.168.1.0/24;
deny all;
}
创建密码文件:
htpasswd -c /etc/nginx/.htpasswd adminuser
map $remote_user $role {
default "guest";
"adminuser" "admin";
"editoruser" "editor";
}
server {
location /admin {
if ($role != "admin") {
return 403;
}
# 管理员内容
}
location /editor {
if ($role != "admin" && $role != "editor") {
return 403;
}
# 编辑内容
}
}
location /secure {
auth_request /auth;
error_page 401 = @error401;
# 如果认证通过,继续处理
...
}
location = /auth {
internal;
proxy_pass http://auth-service/check;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-Method $request_method;
}
location @error401 {
return 302 /login?return=$request_uri;
}
location / {
access_by_lua_block {
local headers = ngx.req.get_headers()
local token = headers["X-Auth-Token"]
if not token then
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
local res = ngx.location.capture("/auth/validate", {
method = ngx.HTTP_GET,
args = { token = token }
})
if res.status ~= ngx.HTTP_OK then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
local role = res.body.role
if not string.find(ngx.var.uri, "^/"..role) then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
}
}
location /api {
access_by_lua_block {
local jwt = require("resty.jwt")
local auth_header = ngx.var.http_Authorization
if auth_header == nil then
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
local _, _, token = string.find(auth_header, "Bearer%s+(.+)")
if token == nil then
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
local jwt_obj = jwt:verify("your-secret-key", token)
if not jwt_obj["verified"] then
ngx.exit(ngx.HTTP_UNAUTHORIZED)
end
-- 检查角色
local required_role = "admin"
if jwt_obj.payload.role ~= required_role then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
}
}
以上方法可以根据您的具体需求和安全要求进行组合和调整。