V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
willhunger
V2EX  ›  Django

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

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

    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 ,就是干
    willhunger
        2
    willhunger  
    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 里面,就能认证了
    willhunger
        4
    willhunger  
    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
    willhunger
        6
    willhunger  
    OP
       2017-04-04 16:20:16 +08:00
    @JasperYanky #5 这个可是基于 jwt 的
    我晚上去试一下吧,不懂再来问
    willhunger
        7
    willhunger  
    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   ·   我们的愿景   ·   实用小工具   ·   2681 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:02 · PVG 00:02 · LAX 09:02 · JFK 12:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.