我正在尝试使用Django rest框架进行JWT令牌认证。我能够成功获得访问权限并刷新令牌。我确保令牌有效。但是当我尝试使用访问令牌访问一些受保护的apiview时。它说
db.donation.aggregate([{$unwind:"$filled"},{$project:{"reqAmt":1,"filledAmt":1} },{$group:{"_id":null,"filledAmt":{"$sum":"$filled.depAmt"}}}])
{"detail":"Authentication credentials were not provided."}.
但是,在服务器端,我确实收到了包含上述令牌的curl -H "Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTE0MzQzNzcxLCJqdGkiOiIwYmE5YTcxZTJmMzQ0YmRmOTM1ZWQ3MTU3ZmI2NDkyZiIsInVzZXJfaWQiOjh9.dI3t8yvNe2Z7MKXojGvFpq_Etf1cLg8QSYsNobJ6jQ0" http://localhost:8000/users/me/
字段的request.META。
我目前正在使用localhost而不是Apache进行开发,具有以下文件和配置:
在views.py中:
HTTP_AUTHORIZAITON
在url.py中:
class GetMyInfo(views.APIView):
def get(self,request):
print(request.META)
user = request.user
profile = user.profile
profile_serializer = ProfileSerializer(instance = profile)
return Response(profile_serializer.data, status = HTTP_200_OK)
settings.py:
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^api/$', get_schema_view()),
re_path(r'^api/auth/', include('rest_framework.urls')),
re_path(r'^api/auth/token/obtain/$', TokenObtainPairView.as_view(), name = 'token_obtain_pair'),
re_path(r'^api/auth/token/refresh/$', TokenRefreshView.as_view(), name = 'token_refresh'),
re_path(r'^api/auth/token/verify/$', TokenVerifyView.as_view(), name = 'token_verify'),
#re_path(r'^api-token-auth/', authviews.obtain_auth_token, name = 'obtain_auth_token'),
re_path(r'^users/$', views.CreateUser.as_view(), name = 'register'),
re_path(r'users/(?P<uuid>[0-9a-f-]+)/$', views.GetUserInfo.as_view(), name = 'info'),
re_path(r'users/me/$', views.GetMyInfo.as_view(), name = 'myinfo'),
]
在models.py中:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api'
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES':(
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES':(
'rest_framework_simplejwt.authentication.JWTAuthentication',
#'rest_framework.authentication.SessionAuthentication',
#'rest_framework.authentication.TokenAuthentication',
#'rest_framework.authentication.BasicAuthentication',
),
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
}
AUTH_USER_MODEL = 'api.User'
答案 0 :(得分:2)
从我看到你使用Public Sub PopulateCombo()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet2") 'change as appropriate
Dim lastRowInD As Long
Dim dedupRange As Range
lastRowInD = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
Set dedupRange = ws.Range("D2:D" & lastRowInD)
dedupRange.RemoveDuplicates Columns:=1, Header:=xlNo
With ws.OLEObjects("ComboBox1").Object
.List = ws.Range("Countries").Value
End With
End Sub
包来处理JWT身份验证。
来自文档的示例指定您应该使用:
rest_framework_simplejwt
访问受保护的视图。
所以而不是
Authorization: Bearer <token>
使用:
curl -H "Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTE0MzQzNzcxLCJqdGkiOiIwYmE5YTcxZTJmMzQ0YmRmOTM1ZWQ3MTU3ZmI2NDkyZiIsInVzZXJfaWQiOjh9.dI3t8yvNe2Z7MKXojGvFpq_Etf1cLg8QSYsNobJ6jQ0" http://localhost:8000/users/me/