简体   繁体   English

如何在flask_httpauth的前端请求中发送用户名和密码?

[英]How do I send username and password in request from frontend for auth with flask_httpauth?

I have an app that implements basic username and password authentication. 我有一个实现基本用户名和密码验证的应用程序。 I wanna be able to send username and password auth headers along with my form data when i make a request from frontend. 当我从前端发出请求时,我希望能够发送用户名和密码auth标头以及表单数据。

how can I send those username and password headers along with the request when form submit is clicked. 单击表单提交后,如何发送用户名和密码标题以及请求。

My verify password looks like this: 我的验证密码如下所示:

@auth.verify_password
def verify_password(username, password):
    from email_scheduler.models.api_models import User
    user = User.query.filter_by(username=username).first()
    if not user or not user.verify_password(password):
        return False
    g.user = user
    return True

My api is this: 我的api是这样的:

class TaskCreate(Resource):
    method_decorators = {'post': [auth.login_required]} #handles auth

    def post(self):
        args = task_parser.parse_args()

        from email_scheduler.models.api_models import TaskModel
        task = TaskModel(task_text=args.get('task_text'), user_id=g.user.id)
        task.save_to_db()

        return make_response(jsonify({'message': 'Task added to the database'}), 200)

It is fairly simple to do with postman, where i can just hop over to the authorization tab and put the username and password in. how do i do it if i have a frontend? 使用邮递员非常简单,我可以跳到授权选项卡,然后输入用户名和密码。如果我有前端,该怎么办?

Unfortunately you cannot explicitly submit additional headers such as Authorization used for basic authentication with a browser issued GET or POST request. 不幸的是,您无法通过浏览器发出的GET或POST请求显式提交其他标头,例如用于基本身份验证的Authorization You can add do this for background requests issued from JavaScript. 您可以添加针对JavaScript发出的后台请求执行此操作。

For plan browser requests, the only way to submit authentication is to force the browser to do it for you by returning a 401 status code from your endpoint. 对于计划的浏览器请求,提交身份验证的唯一方法是通过从端点返回401状态代码来强制浏览器为您执行身份验证。 When the browser gets the 401 it will display a login dialog box where the user enters their username and password. 当浏览器获取401时,它将显示一个登录对话框,用户可以在其中输入用户名和密码。 After that the browser will add an Authorization header to any follow up requests going to that host. 之后,浏览器将向所有发送到该主机的后续请求添加一个Authorization标头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ModuleNotFoundError:没有名为“flask_httpauth”的模块 - ModuleNotFoundError: No module named 'flask_httpauth' Python / Flask - 使用flask_restless和flask_httpauth - Python / Flask - Using flask_restless with flask_httpauth Flask-HTTPAuth verify_password函数未接收用户名或密码 - Flask-HTTPAuth verify_password function not receiving username or password flask-httpauth:get_password装饰器如何用于basic-auth? - flask-httpauth: How is get_password decorator meant to work for basic-auth? 如何在不发送密码 hash 和 salt 的情况下将用户 object(来自 Python Flask)发送回前端(Angular)? - How do i send a User object (from Python Flask) back to the frontend (Angular) without sending the password hash and salt? flask_httpauth装饰器缺少必需的位置参数f - flask_httpauth decorators missing required positional argument f Flask-HTTPAuth:如何将额外的参数传递给用@auth.verify_password 装饰的 function? - Flask-HTTPAuth: how to pass an extra argument to a function decorated with @auth.verify_password? 跨多个 Flask RestPlus 端点文件共享 flask_httpauth 的 HTTPBasicAuth - sharing flask_httpauth 's HTTPBasicAuth across multiple Flask RestPlus endpoint files 如何在 flask restx swagger 中设置基本身份验证的用户名和密码? - How to set username and password for basic auth in flask restx swagger? 如何使用socks5用户名和密码发出requesocks请求? - How do I make a requesocks request with socks5 username and password?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM