Javascript 将敏感信息从Python Flask传递到HTML5 LocalStorage
我正在尝试运行一个flask应用程序,在该应用程序中,我必须将敏感信息存储在用户的浏览器localstorage中,然后再获取它。问题是,我在服务器端有这些敏感信息,显然本地存储只能通过Javascript在客户端访问 所以我的问题是,考虑到我的情况,什么是最好和最安全的方法 这是我所想的一个例子……请告诉我这是否不好,为什么,以及这种方法的任何替代方案Javascript 将敏感信息从Python Flask传递到HTML5 LocalStorage,javascript,python,html,flask,Javascript,Python,Html,Flask,我正在尝试运行一个flask应用程序,在该应用程序中,我必须将敏感信息存储在用户的浏览器localstorage中,然后再获取它。问题是,我在服务器端有这些敏感信息,显然本地存储只能通过Javascript在客户端访问 所以我的问题是,考虑到我的情况,什么是最好和最安全的方法 这是我所想的一个例子……请告诉我这是否不好,为什么,以及这种方法的任何替代方案 #This function gets the sensitive info from an AJAX request via the HTM
#This function gets the sensitive info from an AJAX request via the HTML page
@app.route("/sensitive_info/<string:sensitive_info>", methods=["POST"])
def save_sensitive_info(sensitive_info):
db.save(sensitive_info)
return redirect(url_for('inject_sensitive_info_to_local_storage'))
# This function creates an HTML page that injects the sensitive info using a javascript function with a jinja variable
@app.route("/sensitive_info")
def inject_sensitive_info_to_local_storage():
sensitive_info = [RANDOM_SENSITIVE_INFORMATION]
return render_template("index.html", sensitive_info=sensitive_info)
对于第一个函数,html页面将向我们的烧瓶发送一篇Ajax帖子,发送数据:
$.ajax({url: '/sensitive_info' + localStorage.getItem("sensitive_info")), success: function (data) {console.log("Success");}});
也许您可以使用flask会话在客户端安全地存储信息 配置flask应用程序时,应设置密钥。存储在客户端会话中的信息是加密的,只有知道此密钥才能读取。将密钥设置为相当长的随机数
SECRET_KEY = os.urandom(24)
然后在获取机密信息的例行程序中,您可以添加:
session['secret_info'] = secret_info.
session['secret_info'] = secret_info.