当在其中一个应用程序上调用session.clear时,单个apache服务器上的多个Python Flask应用程序丢失了会话 [英] Multiple Python Flask apps on single apache server losing sessions when session.clear is called on one of the apps

查看:132
本文介绍了当在其中一个应用程序上调用session.clear时,单个apache服务器上的多个Python Flask应用程序丢失了会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个使用WSGI在apache服务器上运行的python 3.6 Flask应用程序.

I have a couple of python 3.6 Flask apps running on my apache server using WSGI.

在同一apache服务器上运行2个不同的应用程序:
www.example.com/lodge
www.example.com/仪表板

There are 2 different apps running on the same apache server:
www.example.com/lodge
www.example.com/dashboard

两个应用都有唯一的 app.secret_key

/dashboard 应用程序是一个烧瓶应用程序,具有自己的一组路由:
/dashboard/login
/dashboard/orders
/dashboard/staff
登录路由调用 session.clear(),并允许用户输入其登录信息.然后,已登录的令牌将存储在会话变量中.
/dashboard/orders dashboard/staff 路由都有一个装饰器,该装饰器检查会话中已登录令牌的存在,如果不存在则重定向到登录路由存在.

The /dashboard app is a flask app with its own set of routes:
/dashboard/login
/dashboard/orders
/dashboard/staff
The login route calls session.clear() and lets the user enter their login information. A logged in token then gets stored in a session variable.
Both the /dashboard/orders and dashboard/staff routes have a decorator which checks for the existence of the logged in token in session and redirects to the login route if it does not exists.

/lodge 应用程序是另一个简单的Flask应用程序,具有自己的路线:
/lodge/welcome
/lodge/personal
/lodge/review
/lodge/confirmation
欢迎路线还会调用 session.clear(),然后显示一个Web表单.当用户提交Web表单时,将调用 personal 路由,该路由将这些Webform输入值存储到会话中.

The /lodge app is another simple Flask app with its own routes:
/lodge/welcome
/lodge/personal
/lodge/review
/lodge/confirmation
The welcome route also calls session.clear() and then displays a webform. When the user submits the webform, the personal route is called which stores those webform input values into session.

我遇到的问题是,如果我导航到 www.example.com/dashboard/login 并登录,那么我可以在工作人员之间滑动并命令路由完全没有问题,但是当然后,我打开一个新选项卡,然后转到 www.example.com/lodge/welcome (然后调用 session.clear ),然后重新打开仪表板选项卡,然后尝试转到到工作人员或订单路线,由于会话已清除,我被重定向回登录路线.

The issue that I am having is if I navigate to www.example.com/dashboard/login and login, I can then flick between the staff and orders routes no problems at all, however when I then open a new tab and go to www.example.com/lodge/welcome (which then calls session.clear) and then reopen the dashboard tab and try to go to the staff or orders route, I get redirected back to the login route as the session has been cleared.

httpd.conf :

<VirtualHost *:80>
    WSGIScriptAlias /newapp "c:/lodge/lodge.wsgi"
    <Directory "c:/lodge">
        Require all granted
    </Directory>

    WSGIScriptAlias /dashboard "c:/dashboard/dashboard.wsgi"
    <Directory "c:/dashboard">
       Require all granted
    </Directory>
</VirtualHost>

旁注,如果我访问 http://example.com/dashboard 上的仪表板应用程序和 http://www.example.com上的lodge应用程序,则不会发生这种情况/lodge

Side note, this does not happen if I access the dashboard app on http://example.com/dashboard and the lodge app on http://www.example.com/lodge

推荐答案

在这里回答我自己的问题.

Answering my own question here.

通过更改两个应用程序的应用程序配置,我很容易在同一个apache服务器上实现了多个应用程序.无需虚拟环境或修改wsgi脚本!

I achieved multiple applications on the same apache server quite easily by changing the app configs for both apps. No virtual enviroments or tinkering with wsgi scripts needed!

住宿应用:

app = Flask(__name__)
app.config.from_object(__name__)

app.config.update(
    SESSION_COOKIE_NAME = 'session_lodge',
    SESSION_COOKIE_PATH = '/lodge/'
)

仪表板应用程序:

app = Flask(__name__)
app.config.from_object(__name__)

app.config.update(
    SESSION_COOKIE_NAME = 'session_db',
    SESSION_COOKIE_PATH = '/dashboard/'
)


@ m-dennis和@ burhan-khalid的回答使人们对这个问题有所了解,所以谢谢!!
拥有多个子域对我来说不是一个选择,当两个应用程序都在各自的虚拟环境中运行时,我遇到了相同的问题.


The answeres by @m-dennis and @burhan-khalid porivded some insight to the problem so thanks for that!
Having multiple sub domains was not an option for me and I encountered the same issue when having both apps run in their own virtual enviroments.

这篇关于当在其中一个应用程序上调用session.clear时,单个apache服务器上的多个Python Flask应用程序丢失了会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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