我有一个烧瓶应用程序,我使用烧瓶登录,按照教程(没有什么花哨在这里)
localhost而不是127.0.0.1.from flask.ext.login import LoginManager
login_manager = LoginManager()
login_manager.session_protection = "strong"
login_manager.init_app(app)
login_manager.login_view = 'login'
def login():
error = None
form = LoginForm()
if request.method == 'POST':
user = db.users.find_one({"username": form.username.data})
pass_hash = generate_password_hash(form.password.data)
if user and User.validate_login( pass_hash, user['password'] ):
user_obj = User(user['username'])
session['logged_in'] = True
login_user(user_obj,remember=True)
flash("Logged in successfully", category='success')
print 'logged in: OK'
#return redirect(request.args.get("next") or url_for("index"))
return redirect( url_for("index"))
error = 'Invalid credentials'
return render_template('login.html', title='login', **locals()) …Run Code Online (Sandbox Code Playgroud) 所以我对Flask会话的理解是我可以像字典一样使用它并通过执行以下操作为会话添加值:
session ['key name'] =''some value here'
这很好.
在路由中我使用AJAX post进行客户端调用,我为会话分配了一个值.它工作正常.我可以点击我网站的各个页面,价值保留在会话中.但是,如果我关闭浏览器窗口,然后返回我的站点,那里的会话值就消失了.
所以这很奇怪,你会认为问题是会话不是永久性的.我还实施烧瓶OpenID和使用该会话来存储信息,并且不坚持,如果我关闭浏览器窗口并打开它再次备份.关闭浏览器窗口后我也检查了cookie,但在回到我的网站之前,cookie确实还在那里.
另一个奇怪的行为(可能是相关的)是我在访问AJAX后期路由并分配正确值时,为了测试目的而写入会话的一些值将消失.所以这很奇怪,但真正奇怪的是当我关闭浏览器窗口并再次打开它,并因此失去了我试图保留的价值时,我之前丢失的那些实际上又回来了!它们没有被重新分配,因为我的Python文件中没有代码可以重新分配这些值.
以下是助手的一些输出使其更清晰.它们都是从真实页面的路线中输出的,而不是我上面提到的AJAX后期路线.
这是我在会话中分配了要存储的值后的输出.值键是'userid' - 所有其他值都是我在尝试解决此问题时添加的虚拟值.'userid':只要我不关闭浏览器窗口,8就会保留在会话中.我可以访问其他路线,价值将保持原样.
['session.=', <SecureCookieSession {'userid': 8, 'test_variable_num': 102, 'adding using before request': 'hi', '_permanent': True, 'test_variable_text': 'hi!'}>]
Run Code Online (Sandbox Code Playgroud)
如果我关闭浏览器窗口,然后返回到站点,但没有重做AJAX发布请求,我得到这个输出:
['session.=', <SecureCookieSession {'adding using before request': 'hi', '_permanent': True, 'yo': 'yo'}>]
Run Code Online (Sandbox Code Playgroud)
'yo'值不在第一个输出中.我不知道它来自哪里.我搜索了我的代码'yo',并没有我在任何地方分配该值的实例.我想我可能在几天前将其添加到会议中.因此它似乎是持久的,但在写入其他值时被隐藏.
最后一个是我再次访问AJAX后期路由,然后转到使用debug打印出密钥的页面.与我上面粘贴的第一个输出相同的输出,你会期望的,'yo'值再次消失(但如果我关闭浏览器窗口它会返回)
['session.=', <SecureCookieSession {'userid': 8, 'test_variable_num': 102, 'adding using before request': 'hi', '_permanent': True, 'test_variable_text': 'hi!'}>]
Run Code Online (Sandbox Code Playgroud)
我在Chrome和Firefox中测试了这个.
所以我觉得这很奇怪,我猜这是因为对会话如何工作的误解.我认为它们是字典,我可以将字典值写入其中并在几天后检索它们,只要我将会话设置为永久性并且cookie不会被删除.
任何想法为什么会发生这种奇怪的行为?
我有一个 Angular 应用程序,它需要调用一个 Flask 服务器,该服务器使用会话在请求之间存储信息。
我还有一个旧的 JS 应用程序,它使用 XMLHttpRequest 调用同一个服务器,我正在用新的 Angular 应用程序替换它。
问题在于,当旧应用程序发出请求时,会话 cookie 会按预期工作,但现在使用 angular 应用程序时却没有。
所有交互都通过本地主机完成。Flask 服务器可从 访问localhost:5000,Angular 应用程序可从localhost:4200.
旧应用程序正在执行这样的请求:
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://localhost:5000/api/getAll", true);
xhttp.withCredentials = true;
xhttp.send();
Run Code Online (Sandbox Code Playgroud)
Angular 应用程序是这样做的:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, } from '@angular/common/http';
import { Observable } from 'rxjs';
const httpOptions = {
withCredentials: true,
headers: new HttpHeaders({
'Content-Type': 'application/json',
'charset': 'UTF-8',
})
};
@Injectable()
export class ServerService {
url …Run Code Online (Sandbox Code Playgroud) 我正在编写一个快速应用程序来查看一个带有一些AJAX样式调用的巨型XML文件viewgroup.我的问题session['groups']不是坚持下去.我有一些旧的阵列只有4个成员卡在某处(cookie?..).view调用时会出现该值.然后,我用最近打开的包含20多个成员的xml文件中的信息覆盖该会话成员.
但是,当viewgroup调用时,会话变量已恢复为旧值,数组中只有4个成员!
代码后跟输出.注意3个sessionStatus()电话
def sessionStatus():
print "# of groups in session = " + str(len(session['groups']))
@app.route('/')
def index():
cams = [file for file in os.listdir('xml/') if file.lower().endswith('xml')]
return render_template('index.html', cam_files=cams)
@app.route('/view/<xmlfile>')
def view(xmlfile):
path = 'xml/' + secure_filename(xmlfile)
print 'opening ' + path
xmlf = open(path, 'r')
tree = etree.parse(xmlf)
root = tree.getroot()
p = re.compile(r'Group')
groups = []
for g in root:
if (p.search(g.tag) is not None) and (g.attrib['Comment'] …Run Code Online (Sandbox Code Playgroud)