我尝试使用Google App Engine模块(https://developers.google.com/appengine/docs/python/modules/)在Google App Engine上创建一个基本应用程序,并在模块之间共享会话信息:
模块:
问题是在仪表板模块中,在登录页面(模块1)中创建的会话数据不存在。
是否可以在Google App Engine中的两个以上模块之间访问会话数据?
来源:
baseHandler.py
import webapp2
from webapp2_extras import sessions
class BaseHandler(webapp2.RequestHandler):
def render_template(self, view_filename, params=None):
params = {}
path = os.path.join(os.path.dirname(__file__), 'views', view_filename)
self.response.out.write(template.render(path, params))
def display_message(self, message):
"""Utility function to display a template with a simple message."""
params = {}
self.render_template('message.html', params)
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()
第1单元(登录)main.py
from google.appengine.ext import ndb
import webapp2
from webapp2_extras.security import hash_password
import logging
import os
import sys
import jinja2
from src.basehandler import BaseHandler
from src.user import User
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape']
)
class MainPage(BaseHandler):
def get(self):
template_values = {}
template = JINJA_ENVIRONMENT.get_template('templates/index.html')
self.response.write( template.render( template_values ) )
def post(self):
email_address = self.request.get('input-email')
password = self.request.get('input-password')
password = hash_password( password, 'sha1', salt=None, pepper=None )
qry = User.query(
ndb.AND(User.email_address == email_address,
User.password == password
)
).fetch()
if qry:
self.session['loggedin'] = True
self.redirect('http://dashboard.myURL.appspot.com/')
else:
self.redirect('/?error=invaliduser')
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key',
}
app = webapp2.WSGIApplication([
('/', MainPage)
], debug=True, config=config )
模块2(信息中心)main.py
from google.appengine.ext import ndb
import webapp2
import logging
import os
import sys
import jinja2
from src.basehandler import BaseHandler
from src.user import User
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape']
)
class Main(BaseHandler):
def get(self):
msg = ''
if not self.session.get('loggedin'):
msg = 'There is not session'
else:
msg = 'There is a session'
template_values = { 'msg': msg }
template = JINJA_ENVIRONMENT.get_template('templates/index.html')
self.response.write( template.render( template_values ) )
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key',
}
app = webapp2.WSGIApplication([
('/', Main)
], debug=True, config=config )
欢迎任何帮助,如果拼写错误,请抱歉
答案 0 :(得分:8)
默认情况下,webapp2_extra.sessions使用基于cooki的会话。这些将绑定到特定域。您的模块可能位于module1.yourapp.appspot.com和module2.yourapp.appspot.com(猜测)。第二个模块无法看到第一个模块设置的cookie。
在您的配置中尝试设置Cookie的域名。
config['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key',
cookie_args': {
'domain' : "yourapp.appspot.com"
}
文档说:
- domain: Domain of the cookie. To work accross subdomains the domain must be set to the main domain with a preceding dot, e.g., cookies set for `.mydomain.org` will work in `foo.mydomain.org` and `bar.mydomain.org`. Default is None, which means that cookies will only work for the current subdomain.
来自:https://code.google.com/p/webapp-improved/source/browse/webapp2_extras/sessions.py
或者您也可以使用其他后端,如内存缓存或数据存储。如果您的会话包含敏感信息,则首选此选项。