settings
INSTALLED_APPS = [
....
'rest_framework.authtoken',
...
]
加载应用了,同步数据库
python manage.py makemigrations
python manage.py migrate
REST_FRAMEWORK = {
# 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
# 新版drf schema_class默认用的是rest_framework.schemas.openapi.AutoSchema
"PAGE_SIZE": 10,
"DEFAULT_PAGINATION_CLASS": "users.pagination.Pagination",
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
# 认证组件
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication'
),
}
路由层
from rest_framework.authtoken import views
urlpatterns += [
url(r'^api-token-auth', views.obtain_auth_token)
]
[root@sdns DjangoProject]# curl -X POST -H "Content-Type: application/json" -d '{"username":"admin","password":"123456"}' http://192.168.16.129:8000/api-token-auth/
{"token":"f7900ec8bb84f820c0f9dc7da87f7ba2c4bdacf1"}[root@sdns DjangoProject]# curl -X POST -H "Cn","password":"123456"}' http://192.168.16.129:8000/api-token-auth/
{"token":"f7900ec8bb84f820c0f9dc7da87f7ba2c4bdacf1"}
多次执行得到token是一样的
[root@sdns DjangoProject]# curl -X POST -H "Cn","password":"123456"}' http://192.168.16.129:8000/api-token-auth/
{"token":"f7900ec8bb84f820c0f9dc7da87f7ba2c4bdacf1"}
[root@sdns DjangoProject]# curl -X POST -H "Cn","password":"123456"}' http://192.168.16.129:8000/api-token-auth/
{"token":"f7900ec8bb84f820c0f9dc7da87f7ba2c4bdacf1"}
[root@sdns DjangoProject]# curl -X POST -H "Cn","password":"123456"}' http://192.168.16.129:8000/api-token-auth/
{"token":"f7900ec8bb84f820c0f9dc7da87f7ba2c4bdacf1"}
得到token就可以得到用户所有权限
[root@sdns DjangoProject]# curl -X GET http://192.168.16.129:8000/users/ -H 'Authorization: Token f7900ec8bb84f820c0f9dc7da87f7ba2c4bdacf1'
{"count":78,"next":"http://192.168.16.129:8000/users/?page=2","previous":null,"results":[{"username":"zhangsan-1","email":"zhangsan-1@mail.com"},{"username":"zhangsan-2","email":"zhangsan-2@mail.com"},{"username":"zhangsan-3","email":"zhangsan-3@mail.com"},{"username":"zhangsan-4","email":"zhangsan-4@mail.com"},{"username":"zhangsan-5","email":"zhangsan-5@mail.com"},{"username":"zhangsan-6","email":"zhangsan-6@mail.com"},{"username":"zhangsan-7","email":"zhangsan-7@mail.com"},{"username":"zhangsan-8","email":"zhangsan-8@mail.com"},{"username":"zhangsan-9","email":"zhangsan-9@mail.com"},{"username":"zha
[root@sdns DjangoProject]#
[root@sdns DjangoProject]# curl -X GET http://192.168.16.129:8000/users/ -H 'Authorization: Token f7900ec8bb84f820c0f9dc7da87f7ba2c4bdacf1'
{"count":78,"next":"http://192.168.16.129:8000/users/?page=2","previous":null,"results":[{"username":"zhangsan-1","email":"zhangsan-1@mail.com"},{"username":"zhangsan-2","email":"zhangsan-2@mail.com"},{"username":"zhangsan-3","email":"zhangsan-3@mail.com"},{"username":"zhangsan-4","email":"zhangsan-4@mail.com"},{"username":"zhangsan-5","email":"zhangsan-5@mail.com"},{"username":"zhangsan-6","email":"zhangsan-6@mail.com"},{"username":"zhangsan-7","email":"zhangsan-7@mail.com"},{"username":"zhangsan-8","email":"zhangsan-8@mail.com"},{"username":"zhangsan-9","email":"zhangsan-9@mail.com"},{"username":"zhangsan-10","email":"zhangsan-10@mail.com"}]}
格式化输出
[root@sdns DjangoProject]# curl -X GET http://192.168.16.129:8000/users/ -H 'Authorization: Token f7900ec8bb84f820c0f9dc7da87f7ba2c4bdacf1' | python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 652 100 652 0 0 10274 0 --:--:-- --:--:-- --:--:-- 10349
{
"count": 78,
"next": "http://192.168.16.129:8000/users/?page=2",
"previous": null,
"results": [
{
"username": "zhangsan-1",
"email": "zhangsan-1@mail.com"
},
{
"username": "zhangsan-2",
"email": "zhangsan-2@mail.com"
},
{
"username": "zhangsan-3",
"email": "zhangsan-3@mail.com"
},
{
"username": "zhangsan-4",
"email": "zhangsan-4@mail.com"
},
{
"username": "zhangsan-5",
"email": "zhangsan-5@mail.com"
},
{
"username": "zhangsan-6",
"email": "zhangsan-6@mail.com"
},
{
"username": "zhangsan-7",
"email": "zhangsan-7@mail.com"
},
{
"username": "zhangsan-8",
"email": "zhangsan-8@mail.com"
},
{
"username": "zhangsan-9",
"email": "zhangsan-9@mail.com"
},
{
"username": "zhangsan-10",
"email": "zhangsan-10@mail.com"
}
]
}
[root@sdns DjangoProject]#