python - 在 Flask 中,有没有办法对除应用程序本身之外的所有人隐藏 @app.route。可以从 JSON 格式的 url 看到我的数据库
问题描述
所以我正在使用 sqlite3、json、axios 和 VUEJS 在 FLASK 服务器上工作。我的 VueJS 前端使用 axios 调用我的 sqlite3 数据库。在烧瓶端,数据库通过@app.route('/api/getdatabase', methods=['GET']) 使用 jsonify 发送。如果我在烧瓶服务器运行时将该 url 插入浏览器,我可以从http://localhost:5000/api/getdatabase看到 JSON 格式的整个数据库。
有没有办法停止从浏览器访问该 url 路由,同时保持与 axios 的“GET”连接处于活动状态?
我附加了 Flask @app.route,以防万一 VueJs 方法在该 Flask 路由上调用“GET”。
@app.route('/api/getdatabase', methods=['GET'])
def database_get():
if request.method == 'GET':
print("/getdatabase record")
conn = sql.connect('test.sqlite3')
cursor = conn.cursor()
cursor.execute("SELECT * FROM DATABASE ORDER By id")
rows = cursor.fetchall()
return jsonify(rows)
getDatabase() {
const path = "http://192.168.1.71:5000/api/getdatabase";
axios
.get(path)
.then(response => {
// console.log(res.data)
this.database = response.data;
})
.catch(error => {
console.log("Failed to get database");
});
},
解决方案
您可以使该端点需要身份验证
from flask import make_response
@app.route('/api/getdatabase', methods=['GET'])
def database_get():
if not request.headers.get('secret') == secret:
return app.response_class(
status=401,
mimetype='application/json',
response=json.dumps({'message': 'Invalid or missing secret'})
)
推荐阅读
- python - Unable to send e-mail using python
- google-cloud-dataflow - CombinePerKey not merging accumulators across multiple workers
- docker - Pod 卡在“CrashLoopBackOff”上,即使它应该进入 /bin/bash
- python - 在 Alpine Linux 中使用与 apk 一起安装的 Python 包
- html - Bootstrap 无法右对齐我的 DIV - 右拉不起作用
- php - Wordpress 插件开发中的错误,包括我的脚本;无法登录
- sql - 此错误“通信链路故障”的原因可能是什么?
- bash - 用于提取开发人员 ID 安装的 CL 选项(策略?)
- typescript - 打字稿中默认值的参数类型
- visio - 如何在 Visio 属性中调整属性框的大小