1. http_auth_request_module
# 版本说明 nginx 1.20.2, 默认自带 http_auth_request_module # 模块查看验证 nginx -v (nginx -v| grep 'http_auth_request_module')
2. 鉴权接口准备
本文后端接口是通过egg.js来实现的
实现代码取决于项目中使用何种后端语言
'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配置
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
auth_request /auth;
传递完整的原始请求uri
- 由于身份验证子请求将丢弃请求体,可以通过以下配置传递信息(本文未使用该方式,故皆已注释);
# 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;
传递校验信息
# url: http://localhost:8080/eggjs?code=202304
# 设置变量 $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
以下配置,本文尚未验证;
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;
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论