简体   繁体   English

DRF JWT 在 OPTIONS 请求上不需要令牌

[英]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.

相关问题 获取DRF-jwt令牌 - Obtaining DRF-jwt token DRF 测试中 jwt 令牌的身份验证错误 401 - Authentication error 401 for jwt token in DRF tests 如何在 drf 中通过访问 jwt 令牌向用户发送它 - how to send user it with access jwt token in drf 何时在 DRF 中的 jwt 中调用 gettoken、刷新令牌和验证令牌? - When to call gettoken, refresh token and verify token by user in jwt in DRF? 在没有 DRF 的表单提交上的 Django 视图中生成 JWT 令牌 - Generate JWT token in Django view on form submit without DRF 如何实际使用 DRF + JWT 身份验证与 Python 请求 - How to actually use DRF + JWT Authentication with Python Requests DRF 序列化器 - 接受字段,但不要在“创建”或“更新”中使用它 - DRF Serializer - Accept field but don't use it in `create` or `update` DRF 简单 jwt。 如何更改来自 TokenObtainPairView 的响应以获取访问令牌过期时间 - DRF simple jwt. How to change response from TokenObtainPairView to get an access token EXPIRES time DRF如何关闭基于JWT的身份验证的CSRF令牌检查? - How does DRF turn off CSRF-token check for JWT-based authentication? DRF 简单 jwt 从用户实例获取身份验证令牌或更改用户名和密码组合 - DRF simple jwt getting auth token from a user instance OR change username and password combination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM