我想建立一个电子商务商店与Django Rest框架在后端和反应JS在前端,我还想为商店建立一个移动应用程序。
我希望api仅供我的React前端客户端和我的移动应用程序访问,但我希望我的客户无需先注册或登录即可看到产品列表。
有没有一种方法可以只验证一个视图的客户端(应用程序本身),并验证用户必须验证的所有其他视图的身份?
是否需要设置API密钥?你们能帮我举几个例子吗?
发布于 2021-03-15 21:08:29
默认情况下,您可以在settings.py文件add中将所有视图定义为“所需权限”:
REST_FRAMEWORK = {
# check that token where provided and who is the user
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication'
),
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
]
}在您希望为所有人打开任何其他视图中,请使用:
class SomeView(APIView):
permission_classes = [permissions.AllowAny]参考文献:
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Authentication
https://stackoverflow.com/questions/66512131
复制相似问题