如果令牌未过期或退出烧瓶,如何跳过登录页面?

时间:2015-08-26 13:56:45

标签: python flask flask-login

如果在烧瓶会话中已经有令牌退出,如何跳过登录页面。在def login():中,我从auth.get_token()方法获得了令牌。如果令牌退出,那么我将如何跳过登录页面。

from flask import session
def authenticated_resource(function):
@wraps(function)
def decorated(*args, **kwargs):
    if session.get("authenticated"):
        return function(*args, **kwargs)
    return abort(403) # unauthenticated
return decorated

@app.route('/login', methods=['GET', 'POST'])
def login():
  error=None
  if request.method=='POST':
    if request.form['username']!='admin' or request.form['password']!='1234':
      error ='Invalid Credentials. Please try again.'
    else:
      username=request.form['username']
      password=request.form['password']      
      token =auth.get_token(username, password)
      session["authenticated"] = True
      session["auth_token"] = token     
      return redirect(url_for('getstats'))
  return render_template('login.html', error=error)


@app.route('/logout')
def logout():
  session.pop("auth_token", None)
  session.pop("authenticated", None)
  return redirect(url_for('login'))



@app.route('/metering')
@authenticated_resource
def getstats():
    token =session.get("auth_token")
    print 'token in metering=',token
    return render_template('metering.html', title='Resource Usage')

0 个答案:

没有答案