[英]Providing 'default' User when not logged in instead of AnonymousUser
Is there a way to globally provide a custom instance of User
class instead of AnonymousUser
?有没有办法全局提供
User
类的自定义实例而不是AnonymousUser
?
It is not possible to assign AnonymousUser
instances when User
is expected (for example in forms, there is need to check for authentication and so on), and therefore having an ordinary User
class with name 'anonymous' (so that we could search for it in the DB) would be globally returned when a non-authenticated user visits the page.不可能在需要
User
时分配AnonymousUser
实例(例如在表单中,需要检查身份验证等),因此有一个名为“anonymous”的普通User
类(以便我们可以搜索它在数据库中)将在未经身份验证的用户访问页面时全局返回。 Somehow implementing a custom authentication mechanism would do the trick?以某种方式实现自定义身份验证机制可以解决问题吗? And I also want to ask if such an idea is a standard approach before diving into this.
在深入研究之前,我还想问问这样的想法是否是一种标准方法。
您可以创建一个自定义中间件(在 AuthenticationMiddleware 之后调用),它检查用户是否登录,如果没有,则用您选择的用户替换附加到请求的当前用户对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.