简体   繁体   English

如何使用LDAP3身份验证保留所有将来请求的用户名

[英]how to persist username for all future requests using LDAP3 authentication

I've written a login.views.py for LDAP3 authentication, and the application is such that once the user login successfully, it will take the user to a template with Welcome "username" displayed on the top right of all future request pages. 我写了一个login.views.py为LDAP3认证,并且应用程序是这样的:一旦用户成功登录,将采取用户与欢迎“用户名”在将来所有的请求页面的右上角显示一个模板。 My application configuration for this login module is __init__.py . 我对该登录模块的应用程序配置是__init__.py Part of the template code regarding whether the user is authenticated is here . 有关用户是否通过身份验证的模板代码的一部分在此处

All of the above codes persist user's name in all future requests (that is different modules of the application) once logged in using Flask's development server; 使用Flask的开发服务器登录后,上述所有代码都会在以后的所有请求(应用程序的不同模块)中保留用户名。 however, when I deployed the application live to production server (nginx, uwsgi) , the username is sometimes persisted and other time not persisted. 但是,当我将应用程序实时部署到生产服务器(nginx,uwsgi)时 ,用户名有时会保留 ,而其他时间则不会保留。

I've followed two previous similar questions on StackOverflow: first question and second question , but still can't understand to solve my problem. 我在StackOverflow上关注了之前的两个类似问题: 第一个问题第二个问题 ,但仍然无法理解来解决我的问题。

How do I persist users' info for all future requests once they successfully login? 用户成功登录后,如何保留用户的信息以用于将来的所有请求?

You cannot use global variables in Flask apps. 您不能在Flask应用中使用全局变量。 You have a users dict to store users fetched from LDAP. 您有一个users来存储从LDAP提取的用户。 During development this "works" because the dev server only uses one process. 在开发过程中,这是“有效的”,因为开发服务器仅使用一个进程。 However, in production you are most likely running the app in more than one process. 但是,在生产中,您最有可能在多个过程中运行该应用程序。 Each process will have its own users dict, so the user will only be loaded if the request is handled by the same process that handled logging in the user. 每个进程都有自己的users字典,因此只有在请求由与处理用户登录相同的进程处理请求时,才会加载用户。

You need to store the user data somewhere separate from the app, where it can be looked up during each request. 您需要将用户数据存储在与应用程序分开的某个地方,可以在每个请求期间查找该数据。 The typical example is a database, but in this case you're using LDAP, just fetch the user data by id from LDAP in the loader function. 典型的示例是数据库,但是在这种情况下,您使用的是LDAP,只需在加载程序功能中通过LDAP从ID中获取用户数据即可。 If you're trying to avoid making queries to LDAP, then you need some other external storage to store and fetch the data, such as a database, memcache, redis, etc. You can also just throw the user data in the session. 如果要避免对LDAP进行查询,则需要其他一些外部存储来存储和获取数据,例如数据库,内存缓存,redis等。您也可以将用户数据放入会话中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM