1安装 nginx 的config 配置 一定要装 redis版本要在1.8.0 以上
cd nginx-1.8.0
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
2.nginx conf配置
location / {
# auth_request_set $access_token $arg_access_token;
auth_request /auth;
auth_request_set $access_token $access_token;
root /;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Allow-Headers *;
#add_header Content-Type "application/octet-stream";
autoindex on;
#index index.html index.htm;
}
location /auth {
internal;
proxy_pass http://192.168.2.60:11011/auth/oauth/getLoadImageToken;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
3 后端java 鉴权接口
@GetMapping("/getLoadImageToken")
public void autLoadImageToken(HttpServletRequest request, HttpServletResponse response) {
try {
String uri = request.getHeader("X-Original-URI");
log.info("nginx请求鉴权的来了请求的url===={}", uri);
String[] access_tokenList = uri.split("=");
String access_token = access_tokenList[access_tokenList.length - 1];
String redisKey = "tofly_oauth:access:" + access_token;
if (null == redis.get(redisKey)) {
response.setStatus(400);
} else {
response.setStatus(200);
}
} catch ( Exception ex ) {
response.setStatus(400);
}
return;
}