我如何使用 jwt 烧瓶进行身份验证

时间:2021-03-17 14:52:42

标签: python jwt ldap

告诉我如何,我在另一条路线中生成的令牌:

@app.route('/login', methods=['POST', 'GET'])
def index():
    # initiate the form..
    form = LoginValidation()
    if request.method == 'POST':
        login_id = form.user_name_pid.data
        login_password = form.user_pid_Password.data
        # create a directory to hold the Logs
        login_msg = global_ldap_authentication(login_id, login_password)
        # validate the connection
        if login_msg == "Success":
            success_message = f"*** Authentication Success"
            additional_claims = {"cn": "some_audience", "name": login_id}
            access_token = create_access_token(identity='user',additional_claims=additional_claims)
            return jsonify(access_token=access_token)
            #return render_template('success.html', success_message=success_message)
        else:
            error_message = f"*** Authentication Failed - {login_msg}"
            return render_template("error.html", error_message=str(error_message))
    return render_template('login.html', form=form)

用于访问页面:

@app.route('/')
@jwt_required()
def gl():
   return render_template('cd.html', results=results_flask)

我有错误: https://localhost:5000 { "msg": "缺少授权头" }

如果在 login.html 上授权成功,实际上如何将 jwt 令牌传递给页面

1 个答案:

答案 0 :(得分:0)

您可以将经过身份验证的请求与返回的 JWT 令牌结合使用。

示例:

curl -X GET -H "Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1Njc0OTcyNjEsImlhdCI6MTU2NzQ5Njk2MSwibmJmIjoxNTY3NDk2OTYxLCJpZGVudGl0eSI6MX0.GncHdTHADlkeXYggn03nnmaS4tCYGNryHP6MQD7NywE" http://localhost:5000/hello

在上面的代码片段中,JWT 令牌在 Authorization 标头中传递。

完整代码可以参考this link