auth_request模块
文件目录
test_1
├── app_auth.py
├── app.py
├── nginx.conf
└── templates├── index.html├── login.html├── t1.html└── t2.html
文件内容
index.html
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>展示页面</title>
</head><body><h1>这是一个需要认证后才能访问的页面</h1>
</body></html>
t1.html
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>展示页面</title>
</head><body><h1>t1t1</h1>
</body></html>
t2.html
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>展示页面</title>
</head><body><h1>t2t2</h1>
</body></html>
app.py
from flask import Flask, render_templateapp = Flask(__name__)@app.route('/')
def index():return render_template('index.html')@app.route('/t1')
def t1():return render_template('t1.html')@app.route('/t2')
def t2():return render_template('t2.html')if __name__ == '__main__':app.run(host='0.0.0.0', port=5000)
app_auth.py
from flask import Flask, request, jsonify
from urllib.parse import urlparse, parse_qsapp = Flask(__name__)@app.route('/auth', methods=['POST'])
def authenticate():# 从请求头中获取 tokentoken = request.headers.get('X-Auth-Token')print("token", token)if token == 'valid_token':return jsonify({'status': 'authenticated'}), 200return jsonify({'status': 'unauthenticated'}), 401if __name__ == '__main__':app.run(host='0.0.0.0', port=5001)
通过url参数传递
http://127.0.0.1:9991/t1?token=valid_token
传递给http://127.0.0.1:5001/auth
配置
proxy_set_header X-Original-URI $request_uri;
nginx.conf
events {worker_connections 1024;
}http {server {listen 9991;server_name 127.0.0.1;location / {auth_request /auth;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://127.0.0.1:5000;}location /auth {proxy_pass http://127.0.0.1:5001/auth;proxy_method POST;proxy_set_header Content-Type application/json;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Original-URI $request_uri;internal;}}
}
app_auth.py
from flask import Flask, request, jsonify
from urllib.parse import urlparse, parse_qsapp = Flask(__name__)@app.route('/auth', methods=['POST'])
def authenticate():# 从url参数中获取token = Noneoriginal_uri = request.headers.get('X-Original-URI')if original_uri:parsed_uri = urlparse(original_uri)query_params = parse_qs(parsed_uri.query)_token = query_params.get('token', [None])[0]token = _tokenprint("token", token)if token == 'valid_token':return jsonify({'status': 'authenticated'}), 200return jsonify({'status': 'unauthenticated'}), 401if __name__ == '__main__':app.run(host='0.0.0.0', port=5001)
通过header传递参数
配置
proxy_set_header X-Auth-Token $http_x_auth_token;
nginx.conf
events {worker_connections 1024;
}http {server {listen 9991;server_name 127.0.0.1;location / {auth_request /auth;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_pass http://127.0.0.1:5000;}location /auth {proxy_pass http://127.0.0.1:5001/auth;proxy_method POST;proxy_set_header Content-Type application/json;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Auth-Token $http_x_auth_token;internal;}}
}
app_auth.py内容
from flask import Flask, request, jsonifyapp = Flask(__name__)@app.route('/auth', methods=['POST'])
def authenticate():# 从请求头中获取 tokentoken = request.headers.get('X-Auth-Token')print("token", token)if token == 'valid_token':return jsonify({'status': 'authenticated'}), 200return jsonify({'status': 'unauthenticated'}), 401if __name__ == '__main__':app.run(host='0.0.0.0', port=5001)