背景:
我知道这是一个非常基本的问题,但我没有前端应用的经验。最近,我尝试从静态HTML页面向云服务器上运行在Docker中的应用程序发送一个GET请求,但没有成功。所以现在,我不再使用HTML页面,而是尝试一个进行身份验证的Flask应用程序。
我想做的事:
创建一个索引页,如果用户登录,就会显示一些产品。如果用户未登录,则会向他们显示登录表单,以键入其电子邮件id和密码。
Flask应用程序将部署在Docker中,并可通过Kubernetes Ingress,在Kubernetes云集群上使用。我主要担心的是用户名和密码将如何从客户端浏览器找到正确的服务。它是通过在HTML页面本身生成的POST 完成的吗?难道POST 请求不需要指向特定的 URL**?吗?当单击“JQuery Login ”按钮时,是否应该由某个**脚本完成密码哈希操作?
我想不出的是:
即使在看到很多例子之后,当用户键入电子邮件和密码并单击“登录”按钮时,我也无法找到正确的散列和发送密码给Flash的方法。然后,如果用户登录,它必须呈现一个HTML页面与产品显示。
如果有一种不使用flask-httpauth (用pip install flask-httpauth安装)的简单方法,或者如果有一个语法更干净的库,那么这种技术也是受欢迎的。
瓶代码:
#!flask/bin/python
from flask import Flask, jsonify, abort, request, make_response, url_for
from flask_httpauth import HTTPBasicAuth
app = Flask(__name__, static_url_path = "")
auth = HTTPBasicAuth()
@app.route('/')
@auth.login_required
def welcome():
return render_template('index.html')
@app.route('/logout')
def welcome():
return render_template('logout.html')
@auth.error_handler
def unauthorized():
return '<!DOCTYPE html><html><body><div style="text-align: center;">Unauthorized access</div></body></html>'
@app.errorhandler(400)
def not_found(error):
return '<!DOCTYPE html><html><body><div style="text-align: center;">Bad request</div></body></html>'
@app.errorhandler(404)
def not_found(error):
return '<!DOCTYPE html><html><body><div style="text-align: center;">Page not found</div></body></html>'
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0")登录页: index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Vacation Finder</title>
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="materialize.min.css"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div>
<nav>
<div class="nav-wrapper grey darken-4">
<div style="text-align: center; font-size: 30px; font-weight: bold;">Some page</div>
<div class="row">
<div class="col s12 m8 l4 offset-m2 offset-l4">
<br>
<br>
<div class="card">
<div class="card-content">
<div class="row">
<div class="input-field col s12">
<input id="email" type="email" class="validate">
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="password" type="password" class="validate">
<label for="password">Password</label>
</div>
</div>
<button id="login" class="waves-effect waves-light btn blue darken-1" style="width:100%;">Login</button>
<br>
</div>
</div>
</div>
</div>
</div>
</nav>
</div>
</body>
</html> 发布于 2021-09-11 12:02:03
如果需要,可以使用JWT令牌。
https://geekflare.com/securing-flask-api-with-jwt/
@app.route('/login', methods=['GET', 'POST'])
def login_user():
auth = request.authorization
if not auth or not auth.username or not auth.password:
return make_response('could not verify', 401, {'WWW.Authentication': 'Basic realm: "login required"'})
user = Users.query.filter_by(name=auth.username).first()
if check_password_hash(user.password, auth.password):
token = jwt.encode({'public_id': user.public_id, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.config['SECRET_KEY'])
return jsonify({'token' : token.decode('UTF-8')})
return make_response('could not verify', 401, {'WWW.Authentication': 'Basic realm: "login required"'})除此之外,您还可以查看官方的bookinfo应用程序istio示例:您也可以检查这个示例:https://github.com/istio/istio/tree/master/samples/bookinfo/src/productpage
JWT:https://medium.com/@apcelent/json-web-token-tutorial-with-example-in-python-df7dda73b579
https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/
https://realpython.com/token-based-authentication-with-flask/
https://stackoverflow.com/questions/69142016
复制相似问题