从 backend.get_user 访问 request.session [英] Access request.session from backend.get_user

查看:21
本文介绍了从 backend.get_user 访问 request.session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:这与这个不同.ModelBackend 没有 request 成员.

first of all: this is not the same as this. The ModelBackend has no request member.

我想访问当前用户的会话,而无需访问全局请求对象或他/她的会话 ID.

I want to access the session of the current user without access to the global request object or his/her session ID.

为什么?我编写了自己的身份验证后端,扩展了 ModelBackend.因为我有函数 get_user (self, user_id),每个请求都会调用它(这是由 Django auth 中间件自动完成的).不幸的是,get_user 无权访问 request,但在我的情况下,我想检查会话中的数据(为什么又一次?因为我没有本地数据库并从远程中间件获取所有数据.会话将是一种进行某种缓存的舒适方式).

Why? I wrote my own authentication backend, extending ModelBackend. In that I have the function get_user (self, user_id), that gets called with every request (this is done automatically by the Django auth middleware). Unfortunately, get_user doesn't have access to request, but nonetheless in my case I want to check with data in the session (Why again? Because I don't have a local database and get all data from a remote middleware. The session would be a comfortable way to do some kind of caching).

推荐答案

RemoteUserBackend(来自 django.contrib.auth.backends) 使用特殊的中间件,RemoteUserMiddleware,访问请求数据.也许你也可以这样做.

The RemoteUserBackend (from django.contrib.auth.backends) uses special middleware, RemoteUserMiddleware, to access request data. Maybe you could do it this way, too.

这篇关于从 backend.get_user 访问 request.session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆