- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个问题
尝试获取 protected /projects/10 URL@admin_permission.require(http_exception=403)
这是我的控制台输出:
127.0.0.1 - - [30/Jul/2013 16:22:58] "GET /projects/10 HTTP/1.1" 302 -
127.0.0.1 - - [30/Jul/2013 16:22:58] "GET /login HTTP/1.1" 200 -
进入登录表单(到目前为止一切正常)。输入有效的登录名和密码并获得疯狂的信号和行为不是我所期望的:
127.0.0.1 - - [30/Jul/2013 16:24:06] "POST /login HTTP/1.1" 302 -
<Employee('103','Dmitry Semenov')>
<Identity id="103" auth_type="None" provides=set([Need(method='role', value='manager'), Need(method='id', value=103L), Need(method='role', value='admin')])>
<flask_login.AnonymousUserMixin object at 0x03258790>
<Identity id="103" auth_type="None" provides=set([])>
127.0.0.1 - - [30/Jul/2013 16:24:06] "GET /projects/10 HTTP/1.1" 302 -
<flask_login.AnonymousUserMixin object at 0x03342AF0>
<Identity id="103" auth_type="None" provides=set([])>
127.0.0.1 - - [30/Jul/2013 16:24:06] "GET /login HTTP/1.1" 200 -
<flask_login.AnonymousUserMixin object at 0x03342E90>
<Identity id="103" auth_type="None" provides=set([])>
如您所见,我得到 current_user 指向有效的 Employee 实例(类)和身份 id=103,但由于某种原因它立即变成了 flask_login.AnonymousUserMixin,然后身份验证系统通过了该用户并且不允许我打开/projects/10 网址。
任何想法有什么问题吗?以及为什么我收到那么多信号 - 根据代码,它们应该只在成功登录时发生。我错过了什么?
源代码:
# flask-principal
principals = Principal()
normal_role = RoleNeed('normal')
normal_permission = Permission(normal_role)
admin_permission = Permission(RoleNeed('admin'))
principals._init_app(app)
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(userid):
return mysqlsess.query(Employee).get(userid)
@app.route("/")
@app.route("/dashboard")
def vDashboard():
return render_template('dashboard.html')
@app.route('/projects')
def vPojects():
return "Projects"
@app.route('/projects/<ID>')
@admin_permission.require(http_exception=403)
def vProject(ID):
return current_user.roles[1]
# somewhere to login
@app.route('/login', methods=['GET', 'POST'])
def login():
# A hypothetical login form that uses Flask-WTF
form = LoginForm()
# Validate form input
if form.validate_on_submit():
# Retrieve the user from the hypothetical datastore
user = mysqlsess.query(Employee).get(form.email.data)
# Compare passwords (use password hashing production)
if form.password.data == str(user.ID):
# Keep the user info in the session using Flask-Login
login_user(user)
# Tell Flask-Principal the identity changed
identity_changed.send(app,
identity=Identity(user.ID))
return redirect(session['redirected_from'] or '/')
else:
return abort(401)
return render_template('login.html', form=form)
# somewhere to logout
@app.route("/logout")
def logout():
logout_user()
for key in ['identity.name', 'identity.auth_type', 'redirected_from']:
try:
del session[key]
except:
pass
return Response('<p>Logged out</p>')
# handle login failed
@app.errorhandler(401)
def page_not_found(e):
return Response('<p>Login failed</p>')
@app.errorhandler(403)
def page_not_found(e):
session['redirected_from'] = request.url
return redirect(url_for('login'))
@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
identity.user = current_user
print identity.user
if hasattr(current_user, 'ID'):
identity.provides.add(UserNeed(current_user.ID))
if hasattr(current_user, 'roles'):
for role in current_user.roles:
identity.provides.add(RoleNeed(role))
print identity
class LoginForm(Form):
email = TextField()
password = PasswordField()
if __name__ == "__main__":
app.run()
还有我的 Employee SQLAlchemy 类
class Employee(Base):
__tablename__ = "Employees"
# Properties
ID = Column(BigInteger, primary_key=True)
name = Column(VARCHAR(255), nullable=False)
created = Column(DateTime, nullable=False, default=datetime.now())
updated = Column(DateTime)
deleted = Column(DateTime)
branchID = Column(BigInteger, ForeignKey('Branches.ID'), nullable=False)
departmentID = Column(BigInteger, ForeignKey('Departments.ID'), nullable=False)
utilization = Column(SmallInteger, nullable=False, default=1)
statusID = Column(Enum('active', 'fired', 'vacation'), default='active')
birthday = Column(Date)
# Relationships
Branch = relationship("Branch")
Department = relationship("Department")
ProjectStat = relationship("ProjectStat", lazy="dynamic")
roles = ["admin", "manager"]
# Methods
def zzz(self):
session = object_session(self)
stats = self.ProjectStat.filter(and_(ProjectStat.metricID=='hb', ProjectStat.metricValue>=6)).all()
for s in stats:
print s.metricValue
# Constructor
def __init__(self, ID, name):
self.ID = ID
self.name = name
# Friendly Print
def __repr__(self):
return "<Employee('%s','%s')>" % (self.ID, self.name)
def is_active(self):
return True
def get_id(self):
return unicode(self.ID)
def is_authenticated(self):
return True
def is_anonymous(self):
return False
最佳答案
登录后需要实例化原理
这是一个重复问题,请参阅此处 Flask Login and Principal - current_user is Anonymous even though I'm logged in
关于python - Flask-Login & Flask-Principle 认证用户掉落到 flask_login.AnonymousUserMixin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17959320/
抱歉,我不知道如何正确表达标题。但我正在制作一个 Java 游戏,屏幕底部有一个桨,您可以左右移动它以避免小行星坠落。我的桨可以工作,但我不知道如何制作一颗下落的小行星。小行星目前只是一个矩形。我还希
我制作了一个 x=25 & y= 25 的正方形并附加了一个 SKPhysicsBody。我制作了一个标准平台关卡,其中有几个高的下降区域。我希望正方形“破裂”/因从高处坠落而受到损坏。我查了一下维基
我是一名初级游戏程序员,我正在学习如何使用适用于 Android 的 AndEngine。我想让一个 Sprite 从屏幕上掉下来,并在用户触摸时向上跳。关于如何做到这一点的任何想法?如果这样更容易,
我刚刚开始在我的 Ogre 项目中实现 bullet。我按照此处的安装说明进行操作:http://www.ogre3d.org/tikiwiki/OgreBullet+Tutorial+1 剩下的教程
将我的项目升级到 iOS7 后 当我执行 BACK 按钮并且 UINavigationController 返回上一页时,屏幕顶部的 ImageView 会向下移动。 我使用 IB 来做我的布局。这些
我有两个内联 div,其中一个内联 div 有两个彼此下方的 div。 JSFiddle 点击#notLoginStudentBtn 我在做什么我正在切换#notLoginStudentBox 但在切
我有一个移动的对象,它生成由滑动关节连接的 object2 当我按下按钮时,关节被移除,object2 在重力作用下落下 这可行,但只在 x 轴上落下而不会保持其 y 轴速度。 如何让 Object2
如何从购物车中删除商品? 自然地,您希望能够将项目拖放回去。 $(function() { $( "#catalog" ).accordion(); $( "#cata
Swift 标准库中是否有作用于集合、采用谓词并返回从该集合中移除的值的函数? 目前,我必须分两步实现它: guard let idx = allAnnotations.index(where: {$
我真的需要一些关于我的游戏对象的帮助。 我正在开发一款游戏,我想要一个拾取元素来产生物理力爆炸来炸毁敌人。我做了一个简单的炸弹对象来测试这个想法。我添加了一个简单的代码,使用一个循环来收集其半径内的所
平台:iOS (Swift)Mapbox SDK版本:3.6 嗨,我有一个自定义 MGLCalloutView,当我点击 MGLAnnotationView 时,标注会按预期显示在注释上方,但如果我稍
我正在使用 WKWebView 查看自定义 HTML。 无论 HTML 内容如何,在真实设备上进行测试时,我都会在 Could not signal service com.apple.WebKi
我正在使用 WKWebView 查看自定义 HTML。 无论 HTML 内容如何,在真实设备上进行测试时,我都会在 Could not signal service com.apple.WebKi
我是一名优秀的程序员,十分优秀!