首页 > 解决方案 > 在 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");
        });
    },

标签: pythonjsonsqliteurlflask

解决方案


您可以使该端点需要身份验证

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'})
        )

推荐阅读