我想使用Django rest-framework Social Oauth2为每个视图或方法设置权限。
我应该如何设置每个视图或方法的权限?
首先,我尝试在settings.py中设置“ DEFAULT_AUTHENTICATION_CLASSES”。但是某些方法不需要身份验证,我想设置AllowAny。因此,我尝试将decorator设置为该方法,但是它不起作用。
settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
'rest_framework_social_oauth2.authentication.SocialAuthentication',
),
}
views.py
@permission_classes([AllowAny, ])
@api_view(['POST'])
def someMethod(request):
return foo
答案 0 :(得分:0)
我自己解决了。在settings.py上评论了DEFAULT_AUTHENTICATION_CLASSES。并将这些Authentification和authentication_classes导入views.py中。
settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
# 'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
# 'rest_framework_social_oauth2.authentication.SocialAuthentication',
),
}
views.py
from rest_framework.decorators import authentication_classes
from oauth2_provider.contrib.rest_framework import OAuth2Authentication
from rest_framework_social_oauth2.authentication import SocialAuthentication
class SampleViewSet(viewsets.ViewSet):
authentication_classes = [OAuth2Authentication, SocialAuthentication]
def someMethodNeedAuth:
return foo
@authentication_classes([AllowAny, ])
@api_view(['POST'])
def someMethodAllowAny(request):
return bar