[英]DRF JWT Don't require token on OPTIONS-requests
How can you make JWT not check and require a token when user does an OPTIONS-request?当用户执行 OPTIONS 请求时,如何让 JWT 不检查并需要令牌?
I have defined my own options(self, request, *args, **kwargs)
method, but as long as the permission_classes = [isAuthenticated]
is enabled, the user is asked to provide login credentials...我已经定义了自己的
options(self, request, *args, **kwargs)
方法,但是只要启用了permission_classes = [isAuthenticated]
,就会要求用户提供登录凭据...
The API we are using in React to interact with DRF has to do an OPTION-request, which does not contain any headers.我们在 React 中用于与 DRF 交互的 API 必须执行一个不包含任何标头的 OPTION-request。 When it does not get a 200 code back, it does not perform the actual request to the server...
当它没有取回 200 代码时,它不会向服务器执行实际请求......
I think the most simple way is to declare custom permissions and use it (in a permissions.py file):我认为最简单的方法是声明自定义权限并使用它(在 permissions.py 文件中):
from rest_framework import permissions
class IsAuthenticated(permissions.IsAuthenticated):
def has_permission(self, request, view):
if request.method == 'OPTIONS':
return True
return super(IsAuthenticated, self).has_permission(request, view)
and in the settings change the DEFAULT_AUTHENTICATION_CLASSES
to this class...并在设置中将
DEFAULT_AUTHENTICATION_CLASSES
更改为此类...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.