1. http_auth_request_module
1 2 | # 版本说明 nginx 1.20.2, 默认自带 http_auth_request_module # 模块查看验证 nginx -V (nginx -V| grep 'http_auth_request_module') |
2. 鉴权接口准备
本文后端接口是通过Egg.js
来实现的
实现代码取决于项目中使用何种后端语言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 'use strict' ; const Controller = require( 'egg' ).Controller; class AuthController extends Controller { async index() { const { ctx } = this ; ctx.body = 'hi, AuthController' ; } async login() { const { ctx, service } = this ; const secrect = Date.now(); const jwt_token = await service.actionToken.apply(secrect); ctx.helper.success(); // ctx.app.jwt.verify(jwt_token, this.app.config.jwt.secret); ctx.body = { status: 200, secret: jwt_token, }; } async authorize() { // 获取 POST 传递的参数 console.log( this .ctx.params); // 获取 GET 传递的参数 console.log( this .ctx.query); // 获取通过 cookie 传递的参数 const isAuthToken = this .ctx.cookies.get( 'authorize' , { signed: false , }); console.log( 'isAuthToken log' , isAuthToken); // 该代码为兼容 4-2 配置代码 console.log( 'x-original-uri' , this .ctx.headers[ 'x-original-uri' ]); if (isAuthToken) { this .ctx.body = { status: 200, secret: 'jwt_token' , }; } else { this .ctx.status = 403; } } } module.exports = AuthController; |
3. 修改Nginx配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location = / { root index; index index.html index.htm; } location /eggjs { if ($http_cookie) { set $code_cookie $http_cookie; } if ($arg_code) { set $code_cookie "authorize=$arg_code; Path=/" ; } # 指定auth_request auth_request /auth ; proxy_pass http: //127 .0.0.1:3000 /static/index .html; } # 验证配置 location /jwt { proxy_pass http: //localhost :3000 /authorize ; } location /scripts { proxy_pass http: //127 .0.0.1:3000 /static/scripts/ ; } location = /auth { internal; # $http_cookie proxy_set_header Cookie "$code_cookie" ; # 鉴权服务器的地址 proxy_pass http: //localhost :3000 /authorize ; # proxy_pass_request_body off; # proxy_set_header Content-Length ""; # proxy_set_header X-Original-URI $query_string; # proxy_set_header X-Original-URI $request_uri; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x .html; location = /50x .html { root html; } } |
4. 配置说明
指定auth_request
1 | auth_request /auth ; |
传递完整的原始请求URI
- 由于身份验证子请求将丢弃请求体,可以通过以下配置传递信息(本文未使用该方式,故皆已注释);
1 2 3 4 | # proxy_pass_request_body off; # proxy_set_header Content-Length ""; # proxy_set_header X-Original-URI $query_string; # proxy_set_header X-Original-URI $request_uri; |
传递校验信息
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 设置变量 $code_cookie,以便后续使用,此处配置兼容 cookie 与 query 两种写法 if ($http_cookie) { set $code_cookie $http_cookie; } if ($arg_code) { # $arg_code 与 URL上的参数code相对应 # 即 secrect=202304 应对应 $arg_secrect set $code_cookie "authorize=$arg_code; Path=/" ; } # 在此处使用变量 $code_cookie proxy_set_header Cookie "$code_cookie" ; |
Extra Config Notes
以下配置,本文尚未验证;
1 2 3 4 5 6 7 8 | location /eggjs { auth_request /auth ; auth_request_set $auth_status $upstream_status; if ($auth_status = "403" ) { return 403; } proxy_pass http: //127 .0.0.1:3000 /static/index .html; } |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。