V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
newbie269
V2EX  ›  Django

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

  •  
  •   newbie269 · Apr 4, 2017 · 6016 views
    This topic created in 3316 days ago, the information mentioned may be changed or developed.

    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 replies    2017-04-04 17:37:10 +08:00
    JasperYanky
        1
    JasperYanky  
       Apr 4, 2017   ❤️ 1
    用了 rest_auth ,就是干
    newbie269
        2
    newbie269  
    OP
       Apr 4, 2017
    @JasperYanky #1 那我现在该如何做?/
    JasperYanky
        3
    JasperYanky  
       Apr 4, 2017
    @willhunger 登录后 拿到 token 然后 request 的时候 放到 head 里面,就能认证了
    newbie269
        4
    newbie269  
    OP
       Apr 4, 2017 via iPhone
    @JasperYanky 直接用表单 post 去认证就好了吧,
    可是我的那个 token 过期和刷新如何解决?
    JasperYanky
        5
    JasperYanky  
       Apr 4, 2017
    @willhunger 过期后只能重新登录 没有过期 可以刷新~ http://getblimp.github.io/django-rest-framework-jwt/#refresh-token
    newbie269
        6
    newbie269  
    OP
       Apr 4, 2017
    @JasperYanky #5 这个可是基于 jwt 的
    我晚上去试一下吧,不懂再来问
    newbie269
        7
    newbie269  
    OP
       Apr 4, 2017
    @JasperYanky #5 https://github.com/aq1/henchman/tree/master/authentication
    麻烦你看看是不是和这个类似,感觉就是写一套 api ,然后在 post 或者 get 得到要的信息?(´・_・`)
    第一次了解这个,不是很懂
    JasperYanky
        8
    JasperYanky  
       Apr 4, 2017
    @willhunger 类似的 自己也可以处理 不过不用自己折腾的话,直接拿来用也蛮好的~ 分分钟插件用起来就是干 :)
    JasperYanky
        9
    JasperYanky  
       Apr 4, 2017
    @willhunger 是的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2424 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 04:40 · PVG 12:40 · LAX 21:40 · JFK 00:40
    ♥ Do have faith in what you're doing.