• 基于 Ajax 请求的 Flask-Login 认证


    index.html
    example.py

    
    ## index.html
    
    ```index.html
    <!DOCTYPE html>
    <html>
        <head>
            <title> test login by Leetao</title>
        </head>
        <body>
            <p id="test_login"></p>
            <button onclick="load_msg()">未点击</button>
            <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            <script>
                var load_msg = function () {
                    $.get('/hello?api_key=test_login',function(data){
                        $('#test_login')[0].innerText = data
                    })
                }
            </script>
        </body>
    </html>
    

    example.py

    from flask import Flask, request, jsonify, render_template
    from flask_login import LoginManager, current_user, login_required
    login_manager = LoginManager()
    
    app = Flask(__name__)
    login_manager.init_app(app)
    
    class User:
        
        def __init__(self,user_name):
            self.id = 'test_id'
            self.user_name = user_name
    
        @property
        def is_active(self):
            return True
    
        @property
        def is_authenticated(self):
            return True
    
        @property
        def is_anonymous(self):
            return False
    
        def get_id(self):
            try:
                return text_type(self.id)
            except AttributeError:
                raise NotImplementedError('No `id` attribute - override `get_id`')
    
    user = User("leetao")
    
    @login_manager.request_loader
    def load_from_request(request):
        api_key = request.args.get('api_key')
        if api_key == 'test_login':
            return user
        return None
    
    @app.route('/hello')
    @login_required
    def hello_world():
        print(current_user.user_name)
        return jsonify('Hello, World!')
    
    @app.route("/")
    def index():
        return render_template("index.html")
    

    结果

    为了方便理解,我截了两张图,一张是 api_key 正确的情况下,一张是 错误的情况下

    api_key 正确

    api_key 错误

    结尾

    关注公众号,不迷路,回复 Python 即可领取 Python 大礼包
  • 相关阅读:
    像素画
    随机世界生成2
    随机世界的生成
    unity2018使用tileMap生成地图 类似泰拉瑞亚创建和销毁地图块
    游戏反编译工具dnSpy
    unity物理学材质Physic Material
    bzoj3261: 最大异或和
    bzoj3524: [Poi2014]Couriers
    hdu2457:DNA repair
    poj2778:DNA Sequence
  • 原文地址:https://www.cnblogs.com/leetao94/p/13219932.html
Copyright © 2020-2023  润新知