Standard 401 response when using HTTP auth in flask

前端 未结 2 1787
广开言路
广开言路 2021-02-05 05:40

In flask, I\'m using the following snippet to enable HTTP auth:

def authenticate():
    return Response(\'\', 401         


        
2条回答
  •  一个人的身影
    2021-02-05 06:04

    Custom error responses are really quite easy in Flask. Create a function whose only argument is the HTTP error status code, make it return a flask.Response instance, and decorate it with @app.errorhandler.

    @app.errorhandler(401)
    def custom_401(error):
        return Response('', 401, {'WWW-Authenticate':'Basic realm="Login Required"'})
    

    You can then use abort(401) to your heart's content.

提交回复
热议问题