当前位置: 首页 > 面试题库 >

Python Flask:跟踪用户会话?如何获取会话Cookie ID?

朱鸿畅
2023-03-14
问题内容

我想在学习活动中构建一个简单的Web应用程序。如果遇到首次访问者,Webapp应该要求用户输入他们的email_id,否则它会通过cookie记住用户并自动登录以执行功能。

这是我第一次创建基于用户的Web应用程序。我心中有一个蓝图,但是我无法弄清楚如何实现它。首先,我对收集用户cookie的方式感到困惑。我研究了各种教程和flask_login,但是与flask_login所实现的相比,我想实现的要简单得多。

我也尝试使用,flask.session但是有点难以理解,最终导致实现有缺陷。

这是到目前为止我所拥有的(它是基本的,目的是传达我的用例):

`from flask import render_template, request, redirect, url_for

@app.route(“/”, methods= [“GET”])
def first_page():
cookie = response.headers[‘cookie’]
if database.lookup(cookie):
user = database.get(cookie) # it returns user_email related to that cookie id
else:
return redirect_url(url_for(‘login’))
data = generateSomeData() # some function
return redirect(url_for(‘do_that’), user_id, data, stats)

@app.route(‘/do_that’, methods =[‘GET’])
def do_that(user_id):
return render_template(‘interface.html’, user_id, stats,data) # it uses Jinja template

@app.route(‘/submit’, methods =[“GET”])
def submit():
# i want to get all the information here
user_id = request.form[‘user_id’]# some data
answer = request.form[‘answer’] # some response to be recorded
data = request.form[‘data’] # same data that I passed in do_that to keep
database.update(data,answer,user_id)
return redirect(url_for(‘/do_that’))

@app.route(‘/login’, methods=[‘GET’])
def login():
return render_template(‘login.html’)

@app.route(‘/loggedIn’, methods =[‘GET’])
def loggedIn():
cookie = response.headers[‘cookie’]
user_email = response.form[‘user_email’]
database.insert(cookie, user_email)
return redirect(url_for(‘first_page’))`


问题答案:

你可以通过request.cookies字典访问请求cookie,并通过使用make_response或仅将调用结果存储render_template在变量中然后调用set_cookie响应对象来设置cookie :

@app.route("/")
def home():
    user_id = request.cookies.get('YourSessionCookie')
    if user_id:
        user = database.get(user_id)
        if user:
            # Success!
            return render_template('welcome.html', user=user)
        else:
            return redirect(url_for('login'))
    else:
        return redirect(url_for('login'))

@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        # You should really validate that these fields
        # are provided, rather than displaying an ugly
        # error message, but for the sake of a simple
        # example we'll just assume they are provided

        user_name = request.form["name"]
        password = request.form["password"]
        user = db.find_by_name_and_password(user_name, password)

        if not user:
            # Again, throwing an error is not a user-friendly
            # way of handling this, but this is just an example
            raise ValueError("Invalid username or password supplied")

        # Note we don't *return* the response immediately
        response = redirect(url_for("do_that"))
        response.set_cookie('YourSessionCookie', user.id)
        return response

@app.route("/do-that")
def do_that():
    user_id = request.cookies.get('YourSessionCookie')
    if user_id:
        user = database.get(user_id)
        if user:
            # Success!
            return render_template('do_that.html', user=user)
        else:
            return redirect(url_for('login'))
    else:
        return redirect(url_for('login'))

DRYing up the code

现在,你会注意到和方法中有很多样板,所有这些都与登录有关。你可以通过编写自己的装饰器来避免这种情况(如果你想了解更多关于装饰器的信息,请参阅什么是装饰器):homedo_that

from functools import wraps
from flask import flash

def login_required(function_to_protect):
    @wraps(function_to_protect)
    def wrapper(*args, **kwargs):
        user_id = request.cookies.get('YourSessionCookie')
        if user_id:
            user = database.get(user_id)
            if user:
                # Success!
                return function_to_protect(*args, **kwargs)
            else:
                flash("Session exists, but user does not exist (anymore)")
                return redirect(url_for('login'))
        else:
            flash("Please log in")
            return redirect(url_for('login'))
    return wrapper

然后,你的home和do_that方法变得更短:

# Note that login_required needs to come before app.route
# Because decorators are applied from closest to furthest
# and we don't want to route and then check login status

@app.route("/")
@login_required
def home():
    # For bonus points we *could* store the user
    # in a thread-local so we don't have to hit
    # the database again (and we get rid of *this* boilerplate too).
    user = database.get(request.cookies['YourSessionCookie'])
    return render_template('welcome.html', user=user)

@app.route("/do-that")
@login_required
def do_that():
    user = database.get(request.cookies['YourSessionCookie'])
    return render_template('welcome.html', user=user)

Using what’s provided

如果你不需要 Cookie来使用特定的名称,我建议你使用flask.session它,因为它已经内置了很多功能(它已签名,因此不能被篡改,可以设置为仅HTTP,等等)。 )。这会使我们的login_required装饰器更加干燥:

# You have to set the secret key for sessions to work
# Make sure you keep this secret
app.secret_key = 'something simple for now' 

from flask import flash, session

def login_required(function_to_protect):
    @wraps(function_to_protect)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        if user_id:
            user = database.get(user_id)
            if user:
                # Success!
                return function_to_protect(*args, **kwargs)
            else:
                flash("Session exists, but user does not exist (anymore)")
                return redirect(url_for('login'))
        else:
            flash("Please log in")
            return redirect(url_for('login'))

然后,你的各个方法可以通过以下方式吸引用户:

user = database.get(session['user_id'])


 类似资料:
  • 问题内容: 如果我想使用Web应用程序跟踪与每个客户端的对话状态,那么哪个更好的替代方法是使用Session Bean还是HTTP Session? 使用HTTP会话: 使用会话EJB: 在执行ServletContextListener时注册为Web应用程序侦听器: 在JSP中: 同一JSP正文中的其他地方: 在我看来,它们几乎是相同的,主要区别在于UserState实例是在前者中传输的,而在后

  • Cookie 通过 HTTP cookie 的会话跟踪是最常用的会话跟踪机制,且所有 servlet 容器都应该支持。 容器向客户端发送一个 cookie,客户端后续到服务器的请求都将返回该cookie,明确地将请求与会话关联。会话跟踪 cookie 的标准名字必须是JSESSIONID。容器也允许通过容器指定的配置自定义会话跟踪cookie的名字。 所有 servlet 容器必须提供能够配置容器

  • 我正在尝试制作一个Minecraft客户端,但我不知道如何获取会话ID来启动游戏。我在google上搜索了一下,但无论如何都找不到从命令行启动Minecraft的答案——用户名和密码作为前缀不起作用。

  • 问题内容: 如何获取烧瓶上的复选框是否已选中我正在使用Flask开发服务器环境使用Flask,Gevent和Web套接字进行项目。我用过。这里 如何获得每个连接的唯一会话ID? 我想将其存储在数据库中,并在客户端断开连接后将其删除。 如何获得活动连接总数 问题答案: 没有会话ID。 Flask中的会话只是Cookie的包装。你保存在上面的内容经过数字签名,并以cookie的形式发送给客户端。当你发

  • 我正在使用Spring Boot、Spring Security性和spring会话(redis)构建spring REST web应用程序。我正在使用SpringCloud和zuul代理按照网关模式构建一个云应用程序。在这个模式中,我使用spring会话来管理redis中的HttpSession,并使用它来授权我的资源服务器上的请求。当执行更改会话权限的操作时,我希望更新该对象,以便用户不必注销

  • 在发布这个问题之前,我谷歌了很多这个问题,并且在网络Socket@ServerEndpoint中从HttpServletRequest访问Http会话是我能找到的最好的问题/答案之一,但它似乎不能解决我的问题。 我可以通过这种方式从websocket访问HttpSession,但是我无法访问与普通HTTP请求相同的CDI会话bean实例。 我也尝试在会话中存储Weld实例,并尝试在WebSocke