我正在实现Django Rest框架JWT以进行令牌认证 . 我正在尝试生成可用于请求受保护资源的JWT令牌,但我收到错误 -
>>> curl -X POST -H "Content-Type: application/json" -d '{"username":"test10@domain.com","password":"welcome!23"}' http://localhost:8000/api-token-auth/
File "<console>", line 1
curl -X POST -H "Content-Type: application/json" -d '{"username":"test10@domain.com","password":"welcome!23"}' http://localhost:8000/api-token-auth/
^
SyntaxError: invalid syntax
>>>
非常感谢您的帮助 .
Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'rest_framework',
...
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.RemoteUserMiddleware', #not sure about it if we need to keep it
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'user.auth.SessionCsrfExemptAuthentication.SessionCsrfExemptAuthentication',
#'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
SessionCsrfExemptAuthentication.py
from rest_framework.authentication import SessionAuthentication
class SessionCsrfExemptAuthentication(SessionAuthentication):
def enforce_csrf(self, request):
return # To not perform the csrf check previously happening
Urls.py
urlpatterns = [
url(r'^users/$', UserViews.UserList.as_view(), name='userlist_view'),
url(r'^users/(?P<pk>[0-9]+)/$', UserViews.UserDetail.as_view(),name='userdetail_view'),
url(r'^api-token-auth/', 'rest_framework_jwt.views.obtain_jwt_token'),
]