请教一个 Django rest_framework 的 Token 认证问题? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
newbie269
V2EX    Django

请教一个 Django rest_framework 的 Token 认证问题?

  •  
  •   newbie269 2017-04-04 14:21:58 +08:00 5864 次点击
    这是一个创建于 3186 天前的主题,其中的信息可能已经有所发展或是发生改变。

    http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication

    这里是官方文档

    Note that the default obtain_auth_token view explicitly uses JSON requests and responses, rather than using default renderer and parser classes in your settings. If you need a customized version of the obtain_auth_token view, you can do so by overriding the ObtainAuthToken view class, and using that in your url conf instead.

    By default there are no permissions or throttling applied to the obtain_auth_token view. If you do wish to apply throttling you'll need to override the view class, and include them using the throttle_classes attribute.

    这两段话不是很理解意思,我先说一下自己的理解,在问问大家是如何想的:

    1. 如果使用内置的 obtain_auth_token view 的和话,请求和相应都是通过 json 数据。 我现在使用 curl post 能够获得到账户的 token
    2. 如果要使用自己的认证方式,就是自己重写 ObtainAuthToken view 这个了类吗? 目前我的一点疑问是,自己定制的认证方式,是普通的表单传递认证还是啥? 如果实现自己的表单认证方式,我要如何去写,在这里好纠结,不知道如何表达
    3. 下面的那段话还不是很理解
    9 条回复    2017-04-04 17:37:10 +08:00
    JasperYanky
        1
    JasperYanky  
       2017-04-04 14:24:20 +08:00   1
    用了 rest_auth ,就是干
    newbie269
        2
    newbie269  
    OP
       2017-04-04 14:31:21 +08:00
    @JasperYanky #1 那我现在该如何做?/
    JasperYanky
        3
    JasperYanky  
       2017-04-04 14:52:36 +08:00
    @willhunger 登录后 拿到 token 然后 request 的时候 放到 head 里面,就能认证了
    newbie269
        4
    newbie269  
    OP
       2017-04-04 15:55:24 +08:00 via iPhone
    @JasperYanky 直接用表单 post 去认证就好了吧,
    可是我的那个 token 过期和刷新如何解决?
    JasperYanky
        5
    JasperYanky  
       2017-04-04 16:06:41 +08:00
    @willhunger 过期后只能重新登录 没有过期 可以刷新~ http://getblimp.github.io/django-rest-framework-jwt/#refresh-token
    newbie269
        6
    newbie269  
    OP
       2017-04-04 16:20:16 +08:00
    @JasperYanky #5 这个可是基于 jwt 的
    我晚上去试一下吧,不懂再来问
    newbie269
        7
    newbie269  
    OP
       2017-04-04 16:43:27 +08:00
    @JasperYanky #5 https://github.com/aq1/henchman/tree/master/authentication
    麻烦你看看是不是和这个类似,感觉就是写一套 api ,然后在 post 或者 get 得到要的信息?(_`)
    第一次了解这个,不是很懂
    JasperYanky
        8
    JasperYanky  
       2017-04-04 16:44:03 +08:00
    @willhunger 类似的 自己也可以处理 不过不用自己折腾的话,直接拿来用也蛮好的~ 分分钟插件用起来就是干 :)
    JasperYanky
        9
    JasperYanky  
       2017-04-04 17:37:10 +08:00
    @willhunger 是的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5137 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 39ms UTC 01:15 PVG 09:15 LAX 17:15 JFK 20:15
    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