如何跳过 httpauth - V2EX
lbfeng
V2EX    Flask

如何跳过 httpauth

  •  
  •   lbfeng Aug 18, 2015 3542 views
    This topic created in 3936 days ago, the information mentioned may be changed or developed.

    调用 restful api 需要通过 httpauth ,而站点使用 flask-login 管理用户登录。如何让已经登录的用户跳过 httpauth 呢? current_user.is_authenticated ()能实现吗?

    @api.before_request
    @auth.login_required
    def before_request ():
    if not g.current_user.is_anonymous and \
    not g.current_user.confirmed:
    return forbidden ('Unconfirmed account')

    Supplement 1    Aug 19, 2015

    补全代码

    @auth.verify_password def verify_password(email_or_token, password): if email_or_token == '': g.current_user = AnonymousUser() return True if password == '': g.current_user = User.verify_auth_token(email_or_token) g.token_used = True return g.current_user is not None user = User.query.filter_by(email=email_or_token).first() if not user: return False g.current_user = user g.token_used = False return user.verify_password(password) 
    1 replies    2015-08-18 13:04:11 +08:00
    jakes
        1
    jakes  
       Aug 18, 2015   1
    我的做法是在 flask-login 登录成功后产生一个用于 httpauth 授权的 token ,保存在 cookie 中。当请求是 RESTful API 时附带上 token 做验证,退出登录时删除该 token 。
    目前没想到什么好方法完美解决,期待楼下提供更为恰当的方法。也许需要写一个包含了 httpauth 授权和 flask-login 功能的 flask 插件吧?
    About     Help     Advertise     Blog     API     FAQ     Solana     2178 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 16:12 PVG 00:12 LAX 09:12 JFK 12:12
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86