一、nginx鉴权1. 依赖模块 依赖模块依赖模块http_auth_request_module验证是否安装nginx -v 2>&1 | grep -- 'http_auth_req
一、nginx鉴权
1. 依赖模块 依赖模块
nginx -v 2>&1 | grep -- 'http_auth_request_module'
2. nginx配置
server
{
listen 80;
location = /checktoken {
internal;
proxy_pass_request_body off;
proxy_set_header content-length "";
proxy_set_header via $request_uri;
proxy_pass $auth_request_url;
}
location = /auth401 {
add_header cache-control "no-store, no-cache, must-revalidate, proxy-revalidate";
if ( $arg_via = "001" ) {
return 401 "{\"msg\":\"登录凭证为空\",\"opcode\":\"001\",\"operatesuccess\":false}";
}
if ( $arg_via = "002" ) {
return 401 "{\"msg\":\"登录凭证失效\",\"opcode\":\"002\",\"operatesuccess\":false}";
}
if ( $arg_via = "003" ) {
return 401 "{\"msg\":\"账户无权限\",\"opcode\":\"003\",\"operatesuccess\":false}";
}
}
location /test/api/ {
set $auth_request_url "http://127.0.0.1:8080/test/api/token/check?token=$arg_token";
auth_request /checktoken;
auth_request_set $auth_via $upstream_http_via;
error_page 401 = /auth401?via=$auth_via;
proxy_pass http://127.0.0.1:8080/test/api/;
}
}
3. rest接口
- 验证的redis账户权限内容
- tokenrest.java
import org.apache.commons.lang3.stringutils;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import javax.annotation.resource;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.util.map;
import java.util.set;
@restcontroller
@requestmapping("/api/token")
public class tokenrest {
@resource
private redistemplate redistemplate;
@getmapping(value="check")
public void checkrest(httpservletrequest request, httpservletresponse response) {
response.setstatus(401);
try {
string url = request.getheader("via");
if (stringutils.isnotempty(url) && url.contains("?")) {
url = url.substring(0, url.indexof("?"));
}
// 白名单跳过验证
string flag = (string) redistemplate.opsforhash().get("whitecache", url);
if (stringutils.isnotempty(flag)) {
response.setstatus(200);
return;
}
// 从head或url中获取token
string token = request.getparameter("token");
if (stringutils.isempty(token) || "null".equals(token)) {
token = request.getheader("authorization");
if (token!=null && token.startswith("bearer ")) {
token = token.substring(7);
}
}
if (stringutils.isempty(token) || "null".equals(token)) {
response.setheader("via", "001");
return;
}
// 从redis中获取账户信息
string accountid = (string) redistemplate.opsforvalue().get(token);
if (accountid == null) {
response.setheader("via", "002");
return;
}
map<string, string> info = (map<string, string>) redistemplate.opsforvalue().get(accountid);
if (info == null) {
response.setheader("via", "003");
return;
}
string[] roleids = info.get("roles").split(",");
for (string roleid : roleids) {
set<string> securityurls = (set<string>) redistemplate.opsforhash().get("funccache",roleid);
if (securityurls.contains(url)) {
flag = "1";
break;
}
}
if ("1".equals(flag)) {
response.setstatus(200);
} else {
response.setheader("via", "003");
}
} catch (exception e) {
system.err.println(e.getmessage());
}
}
}

二、nginx限流
1. 简介
nginx限流是一种用于保护系统资源、防止恶意攻击和控制流量的技术。
- 控制速率:使用
ngx_http_limit_req_module
模块,可以限制每个ip地址单位时间内的请求数。 - 控制连接数:使用
ngx_http_limit_conn_module
模块,可以限制每个ip地址同时保持的连接数。
2. 控制速率
http
{
limit_req_zone $binary_remote_addr zone=limit_req:10m rate=2r/s;
}
项 | 说明 |
---|
binary_remote_addr | 表示通过客户端ip来限制 |
zone | 共享内存区存储访问信息 |
limit_req:10m | 名字为limit_req的内存区域,存储16万ip地址 |
rate=2r/s | 表示每秒最多处理2个请求 |
server
{
location = /test.htm {
limit_req zone=limit_req burst=10 nodelay;
alias c:/nginx/html/test.htm;
}
}
项 | 说明 |
---|
burst=10 | 突发请求不超过10个 |
nodelay | 不延迟处理超过限制的请求 |
3. 控制连接数
http
{
limit_conn_zone $binary_remote_addr zone=limit_conn:10m;
}
项 | 说明 |
---|
binary_remote_addr | 表示通过客户端ip来限制 |
zone | 共享内存区存储访问信息 |
limit_conn:10m | 名字为limit_conn的内存区域,存储16万ip地址 |
server
{
location = /test.htm {
limit_conn limit_conn 2;
alias c:/nginx/html/test.htm;
}
}
项 | 说明 |
---|
limit_conn 2 | 同一个ip地址只允许保持2个连接 |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
相关文章:
-
-
-
-
Nginx安全配置全过程
nginx 是一个高性能的 http 和反向代理服务,使用非常广泛,目前很大一部分网站均使用了 nginx 作为 web 服务器,nginx 虽然非常强大,但是...
[阅读全文]
-
nginx 80端口配置多个location无效 访问404文件目录nginx配置一直感觉配置没啥问题,但是实际访问一直报404,很纳闷,百度了下发现又是一个知识盲区:alias与…
-
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论